Magento – Remove custom Product Attribute from ‘Default’ Attribute Set

magento2

I added a custom product attribute to the default attribute set in Magento 2, but now when I try to remove it, by dragging the property to 'Unassigned Attributes' list, nothing happens. Its not clear how to remove a custom product attribute, once you added it to an attribute set.

I also tried this with a new custom product attribute set, added the custom attribute by dragging it to the Product Details group, saving the changes, and then trying to remove it again, but it does not seem to work.

I am using Magento 2.0.7.

Best Answer

You can remove it by adding the following code to the app/code/MyVendor/MyModule/Setup/InstallData.php file of your module (make sure to change the my_attribute value).

<?php

namespace MyVendor\MyModule\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->removeAttribute(
          \Magento\Catalog\Model\Product::ENTITY,
            'my_attribute');
    }
}

Hope it helps.

Purge cache and deploy static content by running the following commands.

php bin/magento cache:flush
php bin/magento setup:static-content:deploy -f
Related Topic