So far I know that when uninstalling a custom module, it is possible to remove custom tables or columns added by custom module by using uninstall.php which extends \Magento\Framework\Setup\UninstallInterface. But how to remove custom attributes added by InstallData.php when uninstalling the module? Thanks in advance!


In a module, you would use the following code that utilizes dependency injection for uninstallation. It works equally well anywhere else, just be sure to inject the EavSetupFactory into the constructor and then utilize its methods to do the work.


namespace Company\Modulename\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)

            $eavSetup = $this->eavSetupFactory->create();

            $entityTypeId = 1; // Find these in the eav_entity_type table
            $eavSetup->removeAttribute($entityTypeId, 'attribute_code');




Additionally, using this method will cause the eav attribute to properly remove itself from all tables, since they are linked using constraints.

BTW, I recommend using PHPStorm + xdebug. You will learn SO much about how all these things connect together.

  • What file and where does this go? – Mark Aug 25 '16 at 12:27
  • Its Uninstall.php. Its goes in the module setup folder. Check out the namespace. It always should match the pathname. – CarComp Aug 26 '16 at 18:31
  • 1
    You can also use Customer::ENTITY or Product::ENTITY etc. instead of 1 or 4. (use Magento\Catalog\Model\Product; use Magento\Customer\Model\Customer;) – Jānis Elmeris Nov 10 '17 at 8:27

You can use \Magento\Eav\Api\AttributeRepositoryInterface::delete for this.

  • True, but its implied he is building a custom module, so that means assuming the method for creation and deletion is programmatic. Using the api is somewhat the wrong approach, however, you could backtrack your way from the AttributeRepositoryInterface to the class and methods that actually do the work. – CarComp Jul 12 '16 at 20:02
  • 1
    @CarComp, Using the API is only one proper approach, if you interested in work of you module on new version of Magento. Magento use BC policy only for api. And private implementation can be changed at any time without notification – KAndy Jul 13 '16 at 7:01
  • I just re-read what I wrote. I am not saying its the wrong approach for everyone, I meant just in relation to his question. He was asking how to do it in php. – CarComp Jul 13 '16 at 12:12
  • 1
    API - it interfaces with @api annotation, not Web API. Sorry if I something miss understudy – KAndy Jul 13 '16 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.