1

I am experiencing an issue while trying to disable a Magento2 module causing custom values to still be visible in the customer edit page.

I would like to know what i have to do to completely get rid of a module and its data from a Magento2 system.

Magento version: 2.3.2
PHP version: 7.2.19


A custom(own) Magento2 module was installed by:

  • Copying code: app/code/VENDOR/MODULE
  • Running: magento module:enable VENDOR_MODULE
  • Running magento setup:upgrade

This module creates a couple of Customer EAV attributes that correctly show in the customer edit form. I am able to populate/save/update values successfully.


I am disabling the module as such:

  • Running: magento module:disable VENDOR_MODULE
  • Running magento setup:upgrade
  • Completely removing the app/code/VENDOR/MODULE directory

When i navigate back to the customer edit page i can still see the attributes, visible and populated with previously entered data.

At this point i have tried the following:

  • Manually removing the entry in setup_module.
  • Including a Uninstall class.
  • A combination of magento cache:clean && magento setup:di:compile.

Classes attached:

InstallData.php

namespace VENDOR\MODULE\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;

class InstallData implements InstallDataInterface {

    private $customerSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(CustomerSetupFactory $customerSetupFactory) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        $this->installModule1($setup, $context);
        $this->installModule2($setup, $context);
    }

    private function installModule1(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'module1', [
            'type' => 'varchar',
            'label' => 'Module1 label',
            'input' => 'text',
            'source' => '',
            'required' => false,
            'visible' => true,
            'position' => 500,
            'system' => false,
            'backend' => ''
        ]);
        
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'module1')
        ->addData(['used_in_forms' => [
                'adminhtml_customer',
                'adminhtml_checkout',
                'customer_account_create',
                'customer_account_edit'
            ]
        ]);
        $attribute->save();
    }

    private function installModule1(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'module2', [
            'type' => 'varchar',
            'label' => 'Module2 label',
            'input' => 'text',
            'source' => '',
            'required' => false,
            'visible' => true,
            'position' => 500,
            'system' => false,
            'backend' => ''
        ]);
        
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'module2')
        ->addData(['used_in_forms' => [
                'adminhtml_customer',
                'adminhtml_checkout',
                'customer_account_create',
                'customer_account_edit'
            ]
        ]);
        $attribute->save();
    }
}

Uninstall.php

namespace VENDOR\MODULE\Setup;

use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Db\Select;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UninstallInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class Uninstall implements UninstallInterface {
    private $_eavSetupFactory;
    private $_mDSetup;

    public function __construct(EavSetupFactory $eavSetupFactory, ModuleDataSetupInterface $mDSetup) {
        $this->_eavSetupFactory = $eavSetupFactory;
        $this->_mDSetup = $mDSetup;
    }

    public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context) {
        $installer = $setup;
        $eavSetup = $this->_eavSetupFactory->create(['setup' => $this->_mDSetup]);
        $eavSetup->removeAttribute(\Magento\Catalog\Model\Customer::ENTITY, 'module1');
        $eavSetup->removeAttribute(\Magento\Catalog\Model\Customer::ENTITY, 'module2');
    }
}
sm0ke21
  • 441
  • 5
  • 20
  • Have you tried this solution? **https://magento.stackexchange.com/questions/166315/how-can-we-remove-delete-the-custom-attributecreated-for-customer-in-magento-2** – Mohit Rane Jul 08 '19 at 06:47

2 Answers2

0

For Customer attributes you need to delete the specific attributes entry from table "eav_attribute" you can search by "attribute_code" and delete that row, you have to delete attributes from the database because there is no functionality in admin to delete an attribute

nikunj
  • 121
  • 2
0

This is the method by which the custom attribute can be removed as I also tried manually to delete that module attribute and it took more than 1 day to find this solution.

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

        $eavSetup->removeAttribute(Customer::ENTITY, "<attribute name>");

    }

You can remove multiple attribute too at same time by separating attribute name by commas.

After this just run -

 bin/magento setup:upgrade && bin/magento setup:static-content:deploy -f