Hello, we need to create some attributes during the module installation but when we remove the Module the attributes and table remains in Magento. In this post, we will see How we can remove the attribute and table with Uninstaller Script. Please note: Running this script will also remove the data in tables, so only run the Uninstaller command when you don’t need the table data anymore.
Create Uninstall.php at path vendor/module_name/Setup/Patch/Data
<?php /** * Webkul Software. * * @category Webkul * @package Webkul_UninstallerTest * @author Webkul * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ namespace Webkul\UninstallerTest\Setup\Patch\Data; 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 as UninstallInterface; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; /** * Class Uninstall */ class Uninstall implements DataPatchInterface { /** * EAV setup factory * * @var EavSetupFactory */ private $_eavSetupFactory; private $_mDSetup; /** * Init * * @param EavSetupFactory $eavSetupFactory */ public function __construct( EavSetupFactory $eavSetupFactory, ModuleDataSetupInterface $mDSetup, SchemaSetupInterface $setup ) { $this->_eavSetupFactory = $eavSetupFactory; $this->_mDSetup = $mDSetup; $this->setup = $setup; } public function apply() { $setup = $this->setup; $installer = $setup; $installer->startSetup(); /** @var AdapterInterface $connection */ $connection = $installer->getConnection(); $connection->dropTable('wk_test'); // remove table wk_test $installer->endSetup(); /** @var EavSetup $eavSetup */ $eavSetup = $this->_eavSetupFactory->create(['setup' => $this->_mDSetup]); $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'test_uninstall'); // removing the installed attribute } /** * Get Dependecies */ public static function getDependencies() { return []; } /** * Get Aliases */ public function getAliases() { return []; } }
In the above script, I have removed the product attribute ‘test_uninstall’ and table ‘wk_test’.
Be the first to comment.