Magento 2: How to create a console command?

How to create console command in Magento 2-3

We use the command line while developing with Magento 2. For example, From installation to deployment, we use several commands daily.

We also can create our own custom console commands in Magento 2. We can write code that can be executed from the terminal.

As we know, Shell commands are faster than GUI-based applications. Therefore, we can use a console script in the activity in which we don’t require user engagement & require a good amount of operation.

Magento 2: How to create a console command?

We must create di.xml to pass an argument of the class object to the core class Magento\Framework\Console\CommandList.

We need to extend the class Symfony\Component\Console\Command\Command in our custom console class.

The configure function of our custom console class is to provide important information like command name, description and argument list to the console command. The configure function’s type is protected.

The execute() function of the console class is responsible for executing our console command. Therefore, we need to write the logic of our task in this function.

The type of the execute function is protected. This function contains two parameters: Symfony\Component\Console\Input\InputInterface and Symfony\Component\Console\Output\OutputInterface

Let’s check the practicality, of how we can create a console command in Magento 2 (Adobe Commerce).

I have defined di.xml under the etc directory of my module and PHP class for that console command inside the Console directory of my module.

I have added a command to reset the index in the given example.

<?xml version="1.0"?>

<config xmlns:xsi=""
    <type name="Magento\Framework\Console\CommandList">
            <argument name="commands" xsi:type="array">
                <item name="ResetIndexer" xsi:type="object">SbDevBlog\Indexer\Console\ResetIndex</item>


namespace SbDevBlog\Indexer\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use SbDevBlog\Indexer\Services\indexerService;

class ResetIndex extends Command
    private const INDEXER_KEY = "indexerid";

     * @var indexerService
    private indexerService $indexerService;

    public function __construct(
        indexerService $indexerService,
        string         $name = null
        $this->indexerService = $indexerService;

     * Configure command
     * @return void
    protected function configure()
            ->addArgument(self::INDEXER_KEY, InputOption::VALUE_REQUIRED, "Indexer Key")
            ->setDescription("Reset Index Based On Index Id");

     * Resetting index by key
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int|void
    protected function execute(InputInterface $input, OutputInterface $output)
        $indexerId = $input->getArgument(self::INDEXER_KEY);
        if ($this->indexerService->invalidateIndexById($indexerId)) {
            $output->writeln(__("Invalidated %1 successfully", $indexerId));
        $output->writeln(__("Something is wrong, Please check Indexer Id %1", $indexerId));

The addArgument() function allows us to take an argument from the command line. For example, just run the command php bin/magento sbdevblog:resetindex with an argument of index id.


php bin/magento sbdevblog:resetindex catalog_category_product

O/P: Invalidated catalog_category_product successfully

Thanks for reading my blog. Please use the comment section to give your feedback.

To run re-index programmatically, Checkout the post:

Download The Source Code

Note: Please verify the code of this blog and the relevant git repository before using it in production.
sb dev blog adobe commece Magento 2


One thought on “Magento 2: How to create a console command?

Leave a Reply

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