When you should run which commands in Magento 2?
When you should run which commands in Magento 2?

When you should run which commands in Magento 2?

I often see that many new Magento developer run setup upgrade command every time when they changed something from js or HTML.

In this article, I will discuss about which changes need which commands to reflect changes on the frontend or backend of Magento 2.

1) php bin/magento setup:upgrade

Short Form: php bin/magento set:up

You should run this command only on following senarios

  • When you made changes in the Setup script(InstallData, InstallSchema,
    UpgradeData, UpgradeSchem)
  • When you install a new module
  • When you change setup_version in module.xml
  • After upgrade Magento version

2) php bin/magento setup:static-content:deploy

Short Form: php bin/magento s:s:d

You need to run this command only when you made changes in HTML, CSS, JS

If you are in developer mode and enabled symlinks in system you need not run this command. If disabled symlinks, need to delete particular changed files from pub/static folder.

3) php bin/magento setup:di:compile

Short Form: php bin/magento s:d:c

If you made changes like add new dependency in __construct(), changes in di.xml, Add new controller…

If you are in developer mode you can simply delete changed files from var/generation folder for Magento 2.2.x version and /generated folder for Magento 2.3.x

4) php bin/magento cache:flush

Short Form: php bin/magento c:f

You need to run this command, If you made changes in admin configuration, layout xml, ui component, phtml, … file or overrite html, js in frontend theme.

Here is the list of caches in Magento 2

  1. Configuration: After adapting configuration files, it is necessary to flush them including configuration and store specific settings
  2. Layouts: After adapting layout files, it is necessary to flush them including the compiled page layout from all components
  3. Blocks HTML output: After adapting the view layers, it is necessary to flush them including page fragments per block
  4. Collections Data: By Magento, it can flush automatically database queries. However, Custom modules may write entries which make Magento can not clean by itself, in case, Magento can not clean so we need to clean the cache
  5. Reflection Data: API interfaces reflection data will be flushed
    Database DDL operations: it can be flush automatically by Magento, but 3rd party can plus more data, after making custom changes to the database schema, which can clean the cache
  6. EAV types and attributes: The metadata regarding the entity attributes into the cache, in general, it should not flush the cache
  7. Integrations Configuration: Caches the compiled integrations on your store. Clean after adding new or changing existing integrations
  8. Integrations API Configuration: Compiled integration APIs configuration of the Store’s Integrations
  9. Page Cache: This cache links the HTML pages so it is necessary to clean this type of cache regularly
  10. Translations: After merging translations from all modules, the merger cache will be cleaned
  11. Web Services Configuration: Caching the Web API Structure

Here is the list of commands in Magento 2

Common Useful commands:
  • Setup Upgrade: php bin/magento setup:upgrade
  • If you want pub/static files while installing or updating database then use following command:
    php bin/magento setup:upgrade --keep-generated
  • Static Content Deploy(use -f for force deploy on 2.2.x or later):
    php bin/magento setup:static-content:deploy
  • Static Content Deploy For Particular Language:
    php bin/magento setup:static-content:deploy en_US
  • Static Content Deploy For Magento Backend Theme Using Command Line (Working on 2.1.1 or later)
    php 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/another_theme
  • Exclude Themes on Static Content Deploy and does not minify HTML files Using Command Line (Working on 2.1.1 or later)
    php bin/magento setup:static-content:deploy en_US --exclude-theme Magento/luma --no-html-minify
  • Reindex data: php bin/magento indexer:reindex
  • View the list of indexers: php bin/magento indexer:info
  • View indexer status: php bin/magento indexer:status
  • Show the mode of all indexers: php bin/magento indexer:show-mode
Cache related commands:
  • Cache Clean: php bin/magento cache:clean
  • Cache Flush: php bin/magento cache:flush
  • View cache status: php bin/magento cache:status
  • Enable Cache: php bin/magento cache:enable [cache_type]
  • Disable Cache: php bin/magento cache:disable [cache_type]
Modes related commands:
  • Check Current Mode: php bin/magento deploy:mode:show
  • Change To Developer Mode: php bin/magento deploy:mode:set developer
  • Change To Production Mode: php bin/magento deploy:mode:set production
Module related commands:
  • See all modules Status: php bin/magento module:status
  • Enable module: php bin/magento module:enable Namespace_Module
  • Disable module: php bin/magento module:disable Namespace_Module
  • Uninstall Module: php bin/magento module:uninstall Namespace_Module
Site Maintenance Related Commands:
  • Enable Maintenance Mode: php bin/magento maintenance:enable
  • To enable maintenance mode for all clients except 10.10.16.1 and 10.10.16.2: php bin/magento maintenance:enable --ip=10.10.16.1 --ip=10.10.16.2
  • To clear the list of IPs: php bin/magento maintenance:enable --ip=none
  • Disable Maintenance Mode: php bin/magento maintenance:disable
  • Check Maintenance Mode: php bin/magento maintenance:status
  • Allow IP on Maintenance Mode: php bin/magento maintenance:allow-ips --ip=192.0.0.1 --ip=192.0.0.2
Other Commands:
  • Run the single-tenant Compiler: php bin/magento setup:di:compile
  • Unlock Admin User: php bin/magento admin:user:unlock adminusername
  • Set Magento crontab: php bin/magento cron:install --force Use --force to rewrite an existing Magento crontab.
  • To view the crontab, enter the following command as the Magento file system owner. crontab -l
  • Remove Magento crontab: php bin/magento cron:remove

Thank you for reading. Happy coding…!