Read More
Read More
Menu Close

    Uninstalling Module Attributes through Uninstaller Script in Magento 2

    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 any more.

    Create Uninstall.php at path vendor/module_name/Setup/

     * Webkul Software.
     * @category  Webkul
     * @package   Webkul_UninstallerTest
     * @author    Webkul
     * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (
     * @license
    namespace Webkul\UninstallerTest\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 as UninstallInterface;
    use Magento\Eav\Setup\EavSetupFactory;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
     * Class Uninstall
    class Uninstall implements UninstallInterface
         * EAV setup factory
         * @var EavSetupFactory
        private $_eavSetupFactory;
        private $_mDSetup;
         * Init
         * @param EavSetupFactory $eavSetupFactory
        public function __construct(
            EavSetupFactory $eavSetupFactory,
            ModuleDataSetupInterface $mDSetup
            $this->_eavSetupFactory = $eavSetupFactory;
            $this->_mDSetup = $mDSetup;
        public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
            $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

    In the above script, I have removed the product attribute ‘test_uninstall’ and table ‘wk_test’. You can download the complete code from here

    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