Reading list Switch to dark mode

    Uninstalling Module Attributes through Uninstaller Script in Magento 2

    Updated 15 March 2024

    During the installation of the module, it’s essential to generate certain attributes. However, upon removal of the module, the attributes and associated table remain within Magento.

    In this guide, we’ll learn how to remove attributes and tables, which we created during module installation, from Magento upon module uninstallation using an 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

     * Webkul Software.
     * @category  Webkul
     * @package   Webkul_UninstallerTest
     * @author    Webkul
     * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (
     * @license
    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;
            /** @var AdapterInterface $connection */
            $connection = $installer->getConnection();
            $connection->dropTable('wk_test'); // remove table wk_test
            /** @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’.

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*

    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home