Magento2 which command Run and when to Run

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.

Flushing a cache type purges the cache storage, which might affect other processes applications that are using the same storage.

In short: Clean is a soft way of cleaning cache whereas flush is the hard flush of cache storage.

Reference: https://devdocs.magento.com/guides/v2.3/config-guide/cli/config-cli-subcommands-cache.html

Magento 2: What is difference between Cache Clean & Cache Flush?


Although question have already been answered and accepted as well, but i thought it would be nice to compile all points in single answer for better understanding and for other users who are searching for such understanding:

So we will go part by part of question(s):

Question Part A :

Kindly Clear Difference Between php bin/magento cache:flush and cache:clean

as @Shoib Answers, cache:flush destroys the cache storage and build cache again where as cache:clean cleans the cache and doesn't destroy the storage.

Question Part B :

which tasks Output to need cache:flush or clean

when to Cache Clean ?

When you make any change in template, layout or configurations , only clean cache, you should avoid cache:flush in any case. 90% your frontend , configuration issues will be solved by cache:clean.

HINT : During frontend theme development, you can enable all caches just disable full_page cache, this will save you lot of time and in development, you just need to run cache:clean command.

When to Cache Flush ?

Cache flush only and only , if your desired changes not worked as expected after cache:clean command.

Question Part C :

which task need these Commands => **di:compile,setup:upgrade,setup:static-content:deploy

In part c of your question, you mentioned commands like :

1 - **di:compile** , 2 - **setup:upgrade** , 3 - **setup:static-content:deploy**

Let's re-order these as per usage and recommendation of commands :

  • 1 - setup:upgrade

  • 2 - di:compile

  • 3 - setup:static-content:deploy

1 - setup:upgrade :

When you add new module you should run this command this command will add your module entry in setup_module table and also in config.php file. As it updates the Database schema.

2 - setup:di:compile

This command is used to create factories, proxies, interceptors etc :

  • Application code generation (factories, proxies, and so on)
  • Area configuration aggregation (that is, optimized dependency injection configurations per area)
  • Interceptor generation (that is, optimized code generation of interceptors)
  • Interception cache generation
  • Repositories code generation (that is, generated code for APIs)
  • Service data attributes generation (that is, generated extension classes for data objects)

in short run this command when you make any change in your module classes files, constructor etc and generates the contents of the var/di folder in Magento <2.2 and generated for Magento >= 2.2. Refer to Magento Dev Docs for complete detail

3 - setup:static-content:deploy

Run this command when you are developing / making changes in your theme LESS or other JS / static files. Static view files are located in the /pub/static directory, and some are cached in the /var/view_preprocessed directory as well.

For more details about static content refer to Magento Dev Docs here and you can also read in which mode you should run which command.

Note :

If you make any change in template or layout file, php bin/magento cache:clean command will work for you , you don't need to deploy static content.

Hope this will help to understand mechanism.


bin/magento setup:di:compile

This command is used when you make change in di file or create new plugin or preference.

bin/magento setup:upgrade

This command is used when you make a new module and you want to used it in your magento project.

bin/magento indexer:indexer

When you make a change on frontend then you need to use this command.