11

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!

CarComp
1,2561 gold badge17 silver badges36 bronze badges
asked Dec 18, 2015 at 2:18
3
  • Follow this way : magento.stackexchange.com/questions/78221/… Commented Dec 18, 2015 at 4:44
  • @Abdul I've read that post before. But it does not mention the method for removing attributes. Commented Dec 18, 2015 at 8:21
  • Do you mean values in a specific table? Commented Dec 18, 2015 at 15:15

3 Answers 3

12

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.

<?php
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)
 {
 $setup->startSetup();
 $eavSetup = $this->eavSetupFactory->create();
 $entityTypeId = 1; // Find these in the eav_entity_type table
 $eavSetup->removeAttribute($entityTypeId, 'attribute_code');
 $setup->endSetup();
 }
 }
}

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.

answered Jul 12, 2016 at 19:59
4
  • What file and where does this go? Commented Aug 25, 2016 at 12:27
  • Its Uninstall.php. Its goes in the module setup folder. Check out the namespace. It always should match the pathname. Commented Aug 26, 2016 at 18:31
  • 2
    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;) Commented Nov 10, 2017 at 8:27
  • In addition to @JānisElmeris suggestion, consider defining your attribute name as a const in your setup file, so you don't need to use magic strings which can break your application if you decide to change a name and forget to change it in your uninstall script Commented May 5, 2021 at 9:18
3

use Magento\Customer\Model\Customer class instead of entity id like 1 and 2.

<?php
namespace Custom\Module\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;
use Magento\Customer\Model\Customer;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory) 
{
 $this->eavSetupFactory = $eavSetupFactory;
}
 public function install(ModuleDataSetupInterface $setup, ModuleContextInterface 
 $context)
 {
 $setup->startSetup();
 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 $eavSetup->removeAttribute(Customer::ENTITY, 'attribute_code_here');
 $setup->endSetup();
 }
}

Happy Coding !!

answered Jan 23, 2020 at 14:13
1
  • Thanks brother your solution solved my problem! Commented Jan 30, 2020 at 8:01
2

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

answered Jan 15, 2016 at 19:22
4
  • 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. Commented Jul 12, 2016 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 Commented Jul 13, 2016 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. Commented Jul 13, 2016 at 12:12
  • 1
    API - it interfaces with @api annotation, not Web API. Sorry if I something miss understudy Commented Jul 13, 2016 at 13:07

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.