Magento – Magento2: How to remove created eav attribute for which is added through InstallData

deleteeavmagento2.1.0

I have created one product attribute which is not implemeted correctly giving error while add new product in admin , so I want to remove it , I have uninstalled , deleted the module but attribute is still there error is still coming.

I have wriiten for installing :

namespace Vendor\Sorting\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;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        /**
         * Add attributes to the eav/attribute
         */

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'sorting_attribute',
            [
                'type' => 'int',
                'backend' => '',
                'frontend' => '',
                'label' => 'Sorting Attribute',
                'input' => '',
                'class' => '',
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => 0,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'used_for_sort_by' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
    }
}

Best Answer

You can just create Uninstall.php file inside setup folder .

namespace Vendor\Sorting\Setup {

    class Uninstall implements \Magento\Framework\Setup\UninstallInterface
    {    
        protected $eavSetupFactory;

        public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory)
        {
            $this->eavSetupFactory = $eavSetupFactory;
        }

        public function uninstall(\Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context)
        {
            $setup->startSetup();    
            $eavSetup = $this->eavSetupFactory->create();    
            $entityTypeId = 4; // value for eav_entity_type table, here catalog_product value is 4
            $eavSetup->removeAttribute($entityTypeId, 'sorting_attribute');    
            $setup->endSetup();    
        }
    }

}

Run command,

php bin/magento setup:upgrade

Remove var folder and clear cache.

Related Topic