Reading list Switch to dark mode

    Create a custom command to disable a module in Magento 2

    Updated 18 September 2021

    Hello Friends!
    In this blog, we are going to learn how we can create a custom command to disable a custom module with database changes.
    Sometimes, in Magento 2, we create a custom attribute with custom source options, but when we disable the module, we face errors.
    So, here we will add functionality to delete a required custom attributes, tables, or patch names from the database in our custom command.

    Note: If you are using Magento version < 2.3, then the patch_list table will not be available in your database.

    To create a custom command, you have to follow below steps:

    1. Mention your command in di.xml file inside app/code/Vendor/CustomModule/etc/ directory.

    <?xml version="1.0"?>
     * Webkul Software.
     * @category  Webkul
     * @package   Vendor_CustomModule
     * @author    Webkul <[email protected]>
     * @copyright Webkul Software Private Limited (
     * @license ASL Licence
     * @link
    <config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <!--Creating Disable CustomModule Command-->
        <type name="Magento\Framework\Console\CommandList">
                <argument name="commands" xsi:type="array">
                    <item name="disable_custommodule" xsi:type="object">Vendor\CustomModule\Console\Command\DisableCustomModule</item>

    2. Now, we will create DisableCustomModule.php file inside app/code/Vendor/CustomModule/Console/Command/ directory. In this Class file, we will extend Symfony\Component\Console\Command\Command Class and configure our custom command.

     * Webkul Software.
     * @category  Webkul
     * @package   Vendor_CustomModule
     * @author    Webkul
     * @copyright Webkul Software Private Limited (
     * @license
    namespace Vendor\CustomModule\Console\Command;
    use Symfony\Component\Console\Command\Command;
    use Symfony\Component\Console\Input\InputArgument;
    use Symfony\Component\Console\Input\InputOption;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    class DisableCustomModule extends Command
         * @const string
        const REMOVE = 'remove';
         * @var \Magento\Framework\App\ResourceConnection
        protected $resource;
         * @var \Magento\Framework\Module\Manager
        protected $moduleManager;
         * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute
        protected $eavAttribute;
         * @var \Magento\Framework\Module\Status
        protected $modStatus;
         * @param \Magento\Framework\App\ResourceConnection $resource
         * @param \Magento\Framework\Module\Manager         $moduleManager
         * @param \Magento\Eav\Model\Entity\Attribute       $entityAttribute
         * @param \Magento\Framework\Module\Status          $modStatus
        public function __construct(
            \Magento\Framework\Module\Status $modStatus,
            \Magento\Framework\Module\Manager $moduleManager,
            \Magento\Framework\App\ResourceConnection $resource,
            \Magento\Eav\Model\Entity\Attribute $entityAttribute
        ) {
            $this->resource      = $resource;
            $this->moduleManager = $moduleManager;
            $this->eavAttribute  = $entityAttribute;
            $this->modStatus     = $modStatus;
         * {@inheritdoc}
        protected function configure()
            $options = [
                new InputOption(
                ->setDescription('CustomModule Disable Command')
         * {@inheritdoc}
        protected function execute(InputInterface $input, OutputInterface $output)
            try {
                if ($this->moduleManager->isEnabled('Vendor_CustomModule')) {
                    $connection = $this->resource
                    //this code will be execute when you will execute command 'php bin/magento custommodule:disable --remove=tables'
                    if ($input->getOption(self::REMOVE) == "tables") {
                        // drop custom tables
                        // drop custom column from quote_item table
                        $connection->dropColumn($connection->getTableName('quote_item'), 'custom_column');
                        $output->writeln("<info>Database Tables Dropped Succesfully.</info>");
                        $dropColumnsMsg  = "'custom_column' column dropped from quote_item";
                    // delete custom_attribute_of_product product attribute
                    $this->eavAttribute->loadByCode('catalog_product', 'custom_attribute_of_product')->delete();
                    // disable CustomModule module
                    $this->modStatus->setIsEnabled(false, ['Vendor_CustomModule']);
                    // if needed, delete record from patch_list table of CreateCustomAttribute Class
                    $tableName = $connection->getTableName('patch_list');
                    $connection->delete($tableName, "patch_name = 'Vendor\CustomModule\Setup\Patch\Data\CreateAttributes'");
                    // delete entry from setup_module table
                    $tableName = $connection->getTableName('setup_module');
                    $connection->delete($tableName, ["module = 'Vendor_CustomModule'"]);
                    $output->writeln('<info>Vendor_CustomModule module has been disabled successfully.</info>');
            } catch (\Exception $e) {
            return $this;

    3. After creating the above files. Run the following command on CLI.

    php bin/magento setup:di:compile

    4. Now, we can check our custom command in the command list by executing the following command on CLI.

    php bin/magento list

    5. Now, we will run our custom command in CLI.

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    php bin/magento custommodule:disable

    6. Now, we will execute our custom command with parameters as following:

    php bin/magento custommodule:disable --remove=tables

    7. For more information, check here.

    Hope this will be helpful. Thanks ­čÖé

    . . .

    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