Magento 2 provides very powerful symphony based command-line interface that performs installation and configuration tasks. The new command-line interface makes the life of both developers and system administrator much easier to perform multiple tasks like installing Magento, install or upgrade custom or third-party extensions, clearing the cache, managing indexes, deploying static view files, set application mode, create database backups and many more.
This is a major improvement when compared to Magento 1 and it’s highly customizable and extendable. So, you can easily plug in your own commands provided by your Magento 2 extensions.
In this article, we will throw some light on command naming format which will help you to remember it easily and will list out some of the most useful commands in Magento 2.
Command naming format
A command name is a part of the command, which defines behavior of the command on the very high level. In any command it goes right after the command’s name.
For example, in
php bin/magento setup:static-content:deploy
- bin/magento is the command’s name
- setup:static-content:deploy is the name of the command.
If you’ve a Magento installation handy then go to root directory of your installation and enter the following to display the current list of commands:
php bin/magento list
It represents a group of related commands. Commands in a group display in a list, which in turn makes it easier for the user to find the desired command. To find a group name for a command, imagine an subject area where it can be used.
It’s a subject for the action. The subject is optional, but it can be useful for defining sets of commands that work with the same object. If a subject is represented by a compound word, use a dash or hyphen character to separate the words.
It is an action the command does.
General commands: just a group and an action
php bin/magento cache:clean php bin/magento cron:install
Set of commands with a subject
php bin/magento admin:user:create php bin/magento catalog:images:resize
Some most used commands in Magento CLI
Install and upgrade module using command line interface
php bin/magento setup:upgrade
To prevent generated static view files from being deleted when setup:upgrade
php bin/magento setup:upgrade --keep-generated
Display the current running Magento application mode using command line interface
php bin/magento deploy:mode:show
Switch to the developer or production application mode using command line interface
php bin/magento deploy:mode:set developer (or production)
Skips the clearing and regeneration of static content when switch application mode
php bin/magento deploy:mode:set developer --skip-compilation
Deploy static view files in production and default mode using command line interface
php bin/magento setup:static-content:deploy
Deploy static view files in any mode (very useful when need to deploy static content in which application running on developer mode)
php bin/magento setup:static-content:deploy -f
Generate static view files for only the specified themes using command line interface
php bin/magento setup:static-content:deploy --theme Magento/luma --theme VendorName/theme
Generate files only for the specified languages using command line interface
php bin/magento setup:static-content:deploy en_US
Compile dependency injection using command line interface
php bin/magento setup:di:compile
Reindexing all magento indexers using command line interface
php bin/magento indexer:reindex
View the list of indexers using command line interface
php bin/magento indexer:info
Show status of the indexer using command line interface
php bin/magento indexer:status
Show the mode of all indexers using command line interface
php bin/magento indexer:show-mode
Check cache type status using command line interface
php bin/magento cache:status
Enable/Disable all cache types using command line interface
php bin/magento cache:enable (or disable)
Enable/Disable specified cache type using command line interface
php bin/magento cache:disable (or enable) [cache_type]
Magento recommends to disable only the cache types you don’t need while developing for best practice instead of disable all caches. It increases unnecessary load time of all pages and as a result it may also increase your development time.
Enable maintenance mode using command line interface
php bin/magento maintenance:enable
Allow IP on Maintenance mode using command line interface
php bin/magento maintenance:allow-ips --ip=192.0.2.10 --ip=192.0.2.20
Flush or clean magento cache using command line interface
php bin/magento cache:flush (or clean)
Let’s try to understand the main difference between cleaning and flushing cache types.
- Cleaning a cache type deletes all items from enabled Magento cache types only. In other words, this option does not affect other processes or applications because it cleans only the cache that Magento uses. Please note that disabled cache types are not cleaned.
- Flushing a cache type purges the cache storage, which might affect other processes applications that are using the same storage. Flush cache types if you’ve already tried cleaning the cache and you’re still having issues that you cannot isolate.
However, If you find this or anything else difficult in Magento installation, configuration or deployment best practices, we’re happy to help you. Feel free to get in touch!