The Top 7 Magento 2 CLI Commands You Should Know

Top 7 Magento 2 CLI commands You Need To Know

The Magento 2 CLI introduced powerful mode where you can perform various actions. In this article, we will review the 7 most important CLI commands for your store. We will be using for this example Magento 2.1.3 CLI version. Magento team continuously change CLI and this information may change for future releases.

How To Find Out Magento 2 CLI version?

Magento 2 CLI CommandsThe first of all we need to make sure your Magento 2 version at least 2.1.3 and commands below will work.
You will need to connect over ssh to your Magento 2 root folder as Magento file owner.
All commands can be launched as bin/magento {command} or {php_path} bin/magento. In most cases instead of {php_path} you will be able just ot type php.

Once we connected try to run command php bin/magento . If you will see the list of Magento 2 CLI commands then you will have to use php before “bin/magento” for each command.
If you see an error, then maybe few type of issues:
– bin/magento doesn’t have executable rights. (chmod u+x bin/magento)
– you have to use the path to php executable file instead of “php” (which php – will show where it installed)
– Another kind of error. In this case, you can ask your hosting provider what will be the correct way to perform bin/magento command.

Once you got to the list of commands we need to check your Magento 2 CLI version. For this run command:

php bin/magento –version

You will get response something like this:

Magento CLI version 2.1.3

Where 2.1.3 is the version of Magento 2 CLI.

1. php bin/magento maintenance:enable

This command will enable maintenance mode. This is important to do when you upgrade Magento or install new modules. Customers will see maintenance page when the mode will be enabled.

2. php bin/magento setup:upgrade

This command will perform Magento 2 atabase schema and data upgrade. It can be use after installation of an extension or after Magento 2 upgrade.

3. php bin/magento setup:di:compile

After php bin/magento setup:upgrade you will need to re-compile Magento 2 dependancy injection. When you use magento in developer mode you dont need to do it, because in this mode Magento 2 makes symlinks for source files.

4. php bin/magento indexer:reindex

This command will perform full reindex of all Magento 2 indexers. Usually, its done by schedule or on save (can be selected at the admin). But in some cases when you installed an extension or performed Magento 2 upgrade you will need to perform it. Also sometimes you may have an issue where after adding the product you not able to see it in the search results or category page – it also can be fixed by reindex operation.

5. php bin/magento cache:flush

This will flush Magento 2 cache. It can be useful when you not able to flush the cache in the admin. The Magento 2 have a lot of cache types when you use production mode. If you want to remove all cache as well as compiled code and dependency injection you can use the command:

rm -rf var/generation/ var/page_cache/ var/cache/ var/view_preprocessed/ var/report/ var/log/ pub/static/frontend/ pub/static/_cache/ pub/static/adminhtml/ pub/static/_requirejs/

It will clean all cached and generated content and you will need to generate it again (Commands: 3. & 6.)

6. php bin/magento setup:static-content:deploy

This will deploy static content of your stores such as less files compilation, javascript minification, and combination. This need to be done after less/CSS styles updated or in case Magento 2 extension were installed and of course after Magento 2 upgrade.

7. php bin/magento maintenance:disable

This command will disable maintenance mode of Magento 2 store and it will be accessible online again.

The top 7 Magento 2 CLI Commands You Should Know: Video

I hope it will help you to manage your Magento 2 stores. If you have any other useful commands share them at comments.

1 thought on “The Top 7 Magento 2 CLI Commands You Should Know

  1. Pingback: Magento 2.1.3 How To Make Customer Attribute [Update] | Magento Extensions

Leave a Reply

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