0

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 event running just before uninstall started? or any other way to hide the attribute fields when uninstalling the plugin?

3
  • Are you running this file from same module from which you have created it, or another module? Jan 30, 2020 at 12:51
  • The same module. I have a custom module that creating 2 attributes when installing this module (It's working fine). But I would like to remove these attributes when disabling this module. I mean, no need to keep these attributes if the module is disabled.
    – Shabeer M
    Jan 31, 2020 at 5:03
  • I have updated my code, please try now Jan 31, 2020 at 5:41

1 Answer 1

0

Please try below code from new custom module(Uninstall.php):

namespace Vendor\Modulename\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UninstallInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

use Magento\Catalog\Model\Product;

class Uninstall implements UninstallInterface {

    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory) {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface
    $context) {
        $setup->startSetup();

        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->removeAttribute(Product::ENTITY, 'product_attr1');
        $eavSetup->removeAttribute(Product::ENTITY, 'product_attr2');

        $setup->endSetup();
    }

}

Hope this will help you!

5
  • We need to remove attribute in disable a module. not when installing a module. And, I creating the product attributes on installData.php install() function like the following. $eavSetup->addAttribute( ENTITY, 'product_attrone', [ ..... ] ); $eavSetup->addAttribute( ENTITY, 'product_attrtwo', [ ..... ] );
    – Shabeer M
    Jan 30, 2020 at 13:11
  • @ShabeerM you can run InstallData.php file by a new custom module, it will work work Jan 30, 2020 at 13:30
  • Thanks for your reply. The Installdata.php function is not loading while disabling the module. Installdatap.php is running and creating attributes when installing a module (It's working fine). I would like to remove all the additional fields (that created when I install the module) while disable my module.
    – Shabeer M
    Jan 31, 2020 at 4:58
  • After updating the code, you need to run this command: module:uninstall Jan 31, 2020 at 5:48
  • 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. In this situation, how can the plugin user can run the command module:uninstall?
    – Shabeer M
    Feb 3, 2020 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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