0

Magento2 getting error after removing custom module. In this module I have created a custom customer attribute.

ERROR: Class Test\Test\Model\Customer\Attribute\Source\TestAttribute does not exist [] []

Can someone guide me on proper way to remove this module?

Arjun
  • 3,576
  • 23
  • 59

3 Answers3

0

Below File is for remove Custom Attribute

<?php

use Magento\Framework\App\Bootstrap; require DIR . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State'); $state->setAreaCode('adminhtml');

$serviceLocator = new \Zend\ServiceManager\ServiceManager(); $serviceLocator->setService(\Magento\Setup\Mvc\Bootstrap\InitParamListener::BOOTSTRAP_PARAM, []);

$provider = new \Magento\Setup\Model\ObjectManagerProvider($serviceLocator);

$dataSetupFactory = new \Magento\Setup\Module\DataSetupFactory($provider); /** @var \Magento\Framework\Setup\SchemaSetupInterface | \Magento\Framework\Setup\ModuleDataSetupInterface $setup */ $setup = $dataSetupFactory->create(); $eavSetupFactory = new \Magento\Eav\Setup\EavSetupFactory($obj); $eavSetup = $eavSetupFactory->create(['setup' => $setup]);

// For Remove Customer Attribute $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'ATTRIBUTE_CODE');

// For Remove Product Attribute $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'ATTRIBUTE_CODE');

// For Remove Category Attribute $eavSetup->removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'ATTRIBUTE_CODE');

Reference

Msquare
  • 9,063
  • 7
  • 25
  • 63
0

Just run the below Mysql query, it will show you which eav_attribute is using the deleted source class.

SELECT * FROM `eav_attribute` WHERE `source_model` LIKE '%TestAttribute%'

If it is showing an attribute, you need to remove this attribute or set the source_model as NULL.

For removing attributes follow the below answer.

https://magento.stackexchange.com/a/144622/36463

Bilal Usean
  • 9,977
  • 14
  • 75
  • 122
  • Thanks for answering my question. I am aware about deleting using quesry. how can I deleted via uninstall.php script if I haven't uploaded module in packagelist? – Arjun Jul 16 '21 at 09:34
  • Got it. In that case, we can use a custom temporary controller or custom CLI script to delete the attribute. Please refer this answer https://magento.stackexchange.com/a/263826/36463 – Bilal Usean Jul 16 '21 at 09:37
  • CLI i got it but using custom temporary controller. How can i achive this? zlet say even if I create a temp controller where eam I suppose to call it as I need to delete this attributes when some one deletes module manually. – Arjun Jul 16 '21 at 09:43
  • If you don't want to create a custom controller or CLI, then try Magento 2 REST API to delete the attribute. – Bilal Usean Jul 16 '21 at 09:45
0

Following is the way I did, might help someone

use Magento\Framework\App\ResourceConnection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class UninstallPaymentMethod extends Command { const SETUP_MODULE = 'setup_module'; protected $eavSetupFactory; private $state;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
    ResourceConnection $resourceConnection,
    \Magento\Framework\App\State $state
)
{
    parent::__construct('my:first:command');
    $this-&gt;eavSetupFactory = $eavSetupFactory;
    $this-&gt;resourceConnection = $resourceConnection;
    $this-&gt;state = $state;
}

protected function configure() { $this->setName('uninstallpaymentmethod:test'); $this->setDescription('Removing custom customer attribute entry & setup module entry');

   parent::configure();

} protected function execute(InputInterface $input, OutputInterface $output) { $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND); try {

    $connection  = $this-&gt;resourceConnection-&gt;getConnection();
    $tableName = $connection-&gt;getTableName(self::SETUP_MODULE);

    $whereConditions = [
      $connection-&gt;quoteInto('module = ?','Test_Test'),
    ];


    $connection-&gt;delete($tableName, $whereConditions);

    $eavSetup = $this-&gt;eavSetupFactory-&gt;create();

    $entityTypeId = 1; // Find these in the eav_entity_type table
    $eavSetup-&gt;removeAttribute($entityTypeId, 'test1');
    $eavSetup-&gt;removeAttribute($entityTypeId, 'test2');

    $output-&gt;writeln(&quot;Removed module attributes successfully.&quot;);

   } catch (Exception $e) {
       $output-&gt;writeln(&quot;There was some issue in removing attibute.&quot;);
   }

} }

Arjun
  • 3,576
  • 23
  • 59