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!
-
Follow this way : magento.stackexchange.com/questions/78221/…Abdul– Abdul2015年12月18日 04:44:20 +00:00Commented Dec 18, 2015 at 4:44
-
@Abdul I've read that post before. But it does not mention the method for removing attributes.Ricky.C– Ricky.C2015年12月18日 08:21:28 +00:00Commented Dec 18, 2015 at 8:21
-
Do you mean values in a specific table?Maddy– Maddy2015年12月18日 15:15:45 +00:00Commented Dec 18, 2015 at 15:15
3 Answers 3
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.
-
What file and where does this go?Mark– Mark2016年08月25日 12:27:54 +00:00Commented 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.CarComp– CarComp2016年08月26日 18:31:15 +00:00Commented Aug 26, 2016 at 18:31
-
2You can also use
Customer::ENTITYorProduct::ENTITYetc. instead of1or4. (use Magento\Catalog\Model\Product; use Magento\Customer\Model\Customer;)Jānis Elmeris– Jānis Elmeris2017年11月10日 08:27:58 +00:00Commented 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 scriptTschallacka– Tschallacka2021年05月05日 09:18:16 +00:00Commented May 5, 2021 at 9:18
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 !!
-
Thanks brother your solution solved my problem!Faisal Sheikh– Faisal Sheikh2020年01月30日 08:01:35 +00:00Commented Jan 30, 2020 at 8:01
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– CarComp2016年07月12日 20:02:25 +00:00Commented 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 notificationKAndy– KAndy2016年07月13日 07:01:26 +00:00Commented 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.CarComp– CarComp2016年07月13日 12:12:38 +00:00Commented Jul 13, 2016 at 12:12
-
1API - it interfaces with @api annotation, not Web API. Sorry if I something miss understudyKAndy– KAndy2016年07月13日 13:07:34 +00:00Commented Jul 13, 2016 at 13:07
Explore related questions
See similar questions with these tags.