{"id":464,"date":"2019-06-14T10:54:37","date_gmt":"2019-06-14T10:54:37","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/06\/14\/create-console-command-in-magento-2\/"},"modified":"2025-05-22T16:34:29","modified_gmt":"2025-05-22T11:04:29","slug":"create-console-command-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-console-command-in-magento-2\/","title":{"rendered":"How to Create Console Command in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">There are certain tasks that you do in a Magento 2 store that are mundane and repetitive and, unfortunately, they\u2019re also often the most important. For example, running a custom cron after a fixed time period, or remove a directory after caching.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, you can turn many of these boring processes into one-command affairs by using the below method to&nbsp;create console command in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The custom console commands in Magento 2 are very helpful when you want to perform tasks that are repetitive and are to be done in the same manner every time!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, I\u2019ve taken a simple example to display a message when a custom Magento 2 CLI command is run:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Create Console Command in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>registration.php<\/strong>&nbsp;file in&nbsp;<em><strong><em><strong>app\\code\\[Vendor]\\[Namespace]\\<\/strong><\/em><\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n        \\Magento\\Framework\\Component\\ComponentRegistrar::register(\n            \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n            '[Vendor]_[Namespace]',\n            __DIR__\n        );<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>module.xml<\/strong>&nbsp;file in&nbsp;<em><strong><em><strong>app\\code\\[Vendor]\\[Namespace]\\etc<\/strong><\/em><\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n    &lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n        &lt;module name=\"[Vendor]_[Namespace]\" setup_version=\"1.0.0\"\/>\n    &lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<strong>di.xml<\/strong>&nbsp;file in&nbsp;<em><strong><em><strong>app\\code\\[Vendor]\\[Namespace]\\etc<\/strong><\/em><\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n    &lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n        &lt;type name=\"Magento\\Framework\\Console\\CommandList\">\n            &lt;arguments>\n                &lt;argument name=\"commands\" xsi:type=\"array\">\n                    &lt;item name=\"clean\" xsi:type=\"object\">[Vendor]\\[Namespace]\\Model\\Ping&lt;\/item>\n                &lt;\/argument>\n            &lt;\/arguments>\n        &lt;\/type>\n    &lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create&nbsp;<strong>Ping.php<\/strong>&nbsp;in&nbsp;<em><strong><em><strong>[Vendor]\\[Namespace]\\Model<\/strong><\/em><\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\n    namespace [Vendor]\\[Namespace]\\Model;\n\n    use \\Symfony\\Component\\Console\\Command\\Command;\n    use \\Symfony\\Component\\Console\\Input\\InputInterface;\n    use \\Symfony\\Component\\Console\\Output\\OutputInterface;\n\n    class Ping extends Command\n    {\n        protected function configure()\n        {\n            $this->setName('meetanshi:ping')\n                 ->setDescription('Ping us to support!');\n\n            parent::configure();\n        }\n\n        protected function execute(InputInterface $input, OutputInterface $output)\n        {\n            $output->writeln('Need Help Contact To Meetanshi.com!');\n        }\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Run following command:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bin\/magento meetanshi:ping<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The output message will be \u201cNeed Help Contact To Meetanshi.com\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, you can use the function&nbsp;<strong>execute()<\/strong>&nbsp;to&nbsp;<em><strong>create console command in Magento 2<\/strong><\/em>&nbsp;as per your requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are certain tasks that you do in a Magento 2 store that are mundane and repetitive and, unfortunately, they\u2019re also often the most important&#8230;.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-464","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=464"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/464\/revisions"}],"predecessor-version":[{"id":15379,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/464\/revisions\/15379"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}