How to get most out of the bin/magento CLI command in Magento 2
Magento 2 provides a 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 administrators much easier when performing multiple tasks, such as installing Magento, installing or upgrading custom or third-party extensions, clearing the cache, managing indexes, deploying static view files, setting application mode, creating database backups and much more.
This is a major improvement when compared to Magento 1 as 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 highest level. Any command goes right after the command’s name in the code, 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 got 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
Format: group:[subject:]action
group
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.
subject
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.
action
It is an action that the command does.
For examples:
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 of the most frequently used commands in Magento CLI
Install and upgrade a module using the 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 the command line interface
php bin/magento deploy:mode:show
Switch to the developer or production application mode using the command line interface
php bin/magento deploy:mode:set developer (or production)
Skips the clearing and regeneration of static content when switching application mode
php bin/magento deploy:mode:set developer --skip-compilation
Deploy static view files in production and default mode using the command line interface
php bin/magento setup:static-content:deploy
Deploy static view files in any mode (very useful when you need to deploy static content in which an application is running on developer mode)
php bin/magento setup:static-content:deploy -f
Generate static view files for only the specified themes using the command line interface
php bin/magento setup:static-content:deploy --theme Magento/luma --theme VendorName/theme
Generate files only for the specified languages using the command line interface
php bin/magento setup:static-content:deploy en_US
Compile dependency injection using the command line interface
php bin/magento setup:di:compile
Reindexing all magento indexers using the command line interface
php bin/magento indexer:reindex
View the list of indexers using the command line interface
php bin/magento indexer:info
Show the status of the indexer using the command line interface
php bin/magento indexer:status
Show the mode of all indexers using the command line interface
php bin/magento indexer:show-mode
Check cache type status using the command line interface
php bin/magento cache:status
Enable/disable all cache types using the command line interface
php bin/magento cache:enable (or disable)
Enable/disable specified cache type using the command line interface
php bin/magento cache:disable (or enable) [cache_type]
Magento recommends that you disable only the cache types you don’t need while developing for best practice, instead of disabling all caches. This increases unnecessary load time of all pages and as a result it may also increase your development time.
Enable maintenance mode using the command line interface
php bin/magento maintenance:enable
Allow IP on maintenance mode using the command line interface
php bin/magento maintenance:allow-ips --ip=192.0.2.10 --ip=192.0.2.20
Flush or clean the Magento cache using the 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 for 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.
If you find this or anything else difficult in your Magento installation, configuration or deployment best practices, we’re happy to help you. Feel free to get in touch!