Reading list Switch to dark mode

    Uninstalling Module Attributes through Uninstaller Script in Magento 2

    Updated 23 January 2023

    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

     * 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 ?
    Read More
    . . .
    Discuss on Helpdesk

    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