cancel
Showing results for 
Search instead for 
Did you mean: 

How to remove custom product attributes on module disable?

How to remove custom product attributes on module disable?

I added 2 custom product attributes with the help of setup/InstallSchema.php in my Magento 2 custom module. It's working fine.
But, I can't remove these Product attributes on module disable from the site admin module manager section.
I write code for remove attributes in setup/Uninstall.php file like the following

 

public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context )
{
$setup->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'product_attr1');
$eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'product_attr2');

$setup->endSetup();

}

But this is not working when I disable the plugin from admin.

Is any observer/event running just before uninstall started? or any other way to hide the attribute fields when uninstalling the plugin?

 

5 REPLIES 5

Re: How to remove custom product attributes on module disable?

Hello @shabeer_shabeer 

 

when you disable module then it will not run uninstall schema

 

you must need to run

module:uninstall 

command for this

 

more info this:-

https://devdocs.magento.com/guides/v2.3/install-gde/install/cli/install-cli-uninstall-mods.html

Hope it will help you.

 

If works then mark as a solution.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: How to remove custom product attributes on module disable?

Hello @Sunil Patel 

Thank you for your reply.

We can run the command module:uninstall when we develop in localhost. but if we deliver this module to the client (or we enabled the module in the running live site), the client can only disable this plugin from Admin->System->Web Setup Wizard->Module Manager->Disable/Enable, I think they can't run the module:uninstall command. So, how can we overcome this issue? Do we have any running Observer/event in Magento 2 that run just before uninstall a module?

Thanks in Advance

Re: How to remove custom product attributes on module disable?

Hello @shabeer_shabeer 

 

what is the issue if product attribute there, it will create an issue if you are using the source model or backend model.

 

Are you using that?

 

 

Also, that is not recommended way to disable the module.

 

If you don't need to the module then remove module using uninstall 

 

Hope it will help you.

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: How to remove custom product attributes on module disable?

I am creating a custom module, and it will upload in Magento market-place. so anyone can use the plugin. In the current situation, if anyone installed the plugin once in their site 2 additional required fields will be shown in Admin product edit page. And the input fields are showing even the module disabled. So its will make the logical issue because the user won't need the module functionality but the fields should enter when create or edit a product.

Re: How to remove custom product attributes on module disable?

Hello, a vendor installed a module but it was not working.  I will like to uninstall it.

1.  I tried to remove the module with this process

php bin/magento module:status

php bin/magento module:disable module_name (successfully disabled)

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deploy -f

php bin/magento cache:clean

php bin/magento cache:flush

delete the folder from app/code/

ran the above again

2. website loads but get the error

Class vendor_name\module_name\Model\Product\Attribute\Backend\MultiShipping does not exist

Same error occurs in all scripts run in ssh.

How do I remove the module?  It seems to be the problem you mention with a custom class model backend

 

Thanks in advance