Magento 2: Command Line

Useful Commands & How To Use Them

To get the most out of your Magento 2 installation and the functionality of this CMS, it is best to learn some of the commands in the Magento 2 CLI (command line).

Invoking the Magento CLI is a little different between the Linux and Windows OS:

  • Linux OS: access the server through SSH and navigate to the webroot Magento directory. In a “cPanel/WHM - CentOS” system that would be the /home/username/webroot/path/here/ directory. Once you are in the Magento root you can run commands like:

    php bin/magento indexer:reindex
    
  • Windows OS: access the server through RDP then open a CMD or PowerShell prompt and navigate to the PHP version directory that the client site is currently set to. For example “C:\PHP\7.0” and then run commands like:

    ./php.exe C:\home\[insert domain here]\wwwroot\bin\magento indexer:reindex
    

Note: The biggest difference between the two systems is that PHP is assumed as an environment variable in our Linux template where in the Windows template you will need to initiate the Magento commands from the PHP directory that the site is using.

The following is a list of useful commands for a variety of situations including enabling/disable functionality, deploying page/theme content, and invoking instance modes:

Note: In each example, we are listing the Linux command first then the Windows command.

Upgrading:

Setup Upgrade

php bin/magento setup:upgrade
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento setup:upgrade

If you don’t want to remove pub/static files while installing/updating database then use the following command.

php bin/magento setup:upgrade --keep-generated
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento setup:upgrade --keep-generated

Cache Management:

Cache Clean

php bin/magento cache:clean
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento cache:clean

Cache Flush

php bin/magento cache:flush
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento cache:flush

View cache status

php bin/magento cache:status
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento cache:status

Enable Cache

php bin/magento cache:enable [cache_type]
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento cache:enable [cache_type]

Disable Cache

php bin/magento cache:disable [cache_type]
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento cache:disable [cache_type]

Content Deployment:

Static Content Deploy

php bin/magento setup:static-content:deploy
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento setup:static-content:deploy

Static Content Deploy For Particular Language

php bin/magento setup:static-content:deploy en_US
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento setup:static-content:deploy en_US

Static Content Deploy For Magento Backend Theme (Working on 2.1.1 or later)

php bin/magento setup:static-content:deploy --theme="Magento/backend"
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento setup:static-content:deploy --theme="Magento/backend"

Static Content Deploy For Specific Themes (Working on 2.1.1 or later)

php bin/magento setup:static-content:deploy --theme Magento/luma --theme Magento/second_theme
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento setup:static-content:deploy --theme Magento/luma --theme Magento/second_theme

Exclude Themes on Static Content Deploy and does not minify HTML files (Working on 2.1.1 or later)

php bin/magento setup:static-content:deploy en_US --exclude-theme Magento/luma --no-html-minify
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento setup:static-content:deploy en_US --exclude-theme Magento/luma --no-html-minify

Indexing:

Reindexing

php bin/magento indexer:reindex
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento indexer:reindex

View the list of indexers

php bin/magento indexer:info
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento indexer:info

View indexer status

php bin/magento indexer:status
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento indexer:status

Show the mode of all indexers

php bin/magento indexer:show-mode
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento indexer:show-mode

Module Management:

Enable module

php bin/magento module:enable Namespace_Module
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento module:enable Namespace_Module

Disable module

php bin/magento module:disable Namespace_Module
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento module:disable Namespace_Module

Uninstall Module

php bin/magento module:uninstall Namespace_Module
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento module:uninstall Namespace_Module

Instance Mode Manipulation:

Check Current Mode

php bin/magento deploy:mode:show
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento deploy:mode:show

Change To Developer Mode

php bin/magento deploy:mode:set developer
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento deploy:mode:set developer

Change To Production Mode

php bin/magento deploy:mode:set production
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento deploy:mode:set production

Change To Maintenance Mode

php bin/magento maintenance:enable
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento maintenance:enable

Disable Maintenance Mode

php bin/magento maintenance:disable
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento mainenacne:disable

Authorize IP in Maintenance Mode

php bin/magento maintenance:allow-ips 123.123.123.123
./php.exe C:\home\[insert domain here]\wwwroot\bin\magento maintenance:allow-ips 123.123.123.123