1

I created 4 custom attributes for category. Now I want to remove it. I want to use object manager for delete attributes and its values.

See this image. Created attributes are display in this manner. enter image description here

Now I created one file delete_attribute.php in magento root folder.

<?php
ini_set('display_errors', 1);
//ini_set('memory_limit','-1');
error_reporting(E_ALL);
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$storeManager = $obj->get('\Magento\Store\Model\StoreManagerInterface');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$state = $objectManager->get('Magento\Framework\App\State');
//$state->setAreaCode('frontend');
$state->setAreaCode('adminhtml');
// 165
// attribute_code1
// \Magento\Catalog\Model\Category
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('attribute_code1'); //Get Current Category
$PCategory = $objectManager->get('Magento\Catalog\Model\CategoryFactory')->create()->load(165);
echo $PCategory->getIsCollectionPageListing();
print_r($PCategory);
?>

Does anyone know how to delete these 4 attributes using object manager ?

Thanks in advance.

asked Oct 24, 2019 at 8:03

1 Answer 1

0

Create ../Setup/UpgradeData.php file:

<?php
namespace Vendor\Module\Setup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
class UpgradeData implements UpgradeDataInterface
{
 private $eavSetupFactory;
 /**
 * UpgradeData constructor.
 * @param EavSetupFactory $eavSetupFactory
 */
 public function __construct(EavSetupFactory $eavSetupFactory)
 {
 $this->eavSetupFactory = $eavSetupFactory;
 }
 /**
 * @param ModuleDataSetupInterface $setup
 * @param ModuleContextInterface $context
 */
 public function upgrade(
 ModuleDataSetupInterface $setup,
 ModuleContextInterface $context
 ) {
 $setup->startSetup();
 if (version_compare($context->getVersion(), '1.0.1') < 0) { // 1.0.1 is your new version in module.xml
 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 $eavSetup->removeAttribute(
 \Magento\Customer\Model\Customer::ENTITY,
 'sample_attribute'
 );
 }
 $setup->endSetup();
 }
}
answered Oct 24, 2019 at 9:38
1
  • I want it using object manager. Commented Oct 24, 2019 at 10:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.