>A Short Way to Know Magento 2 Clear Cache Command Line

A Short Way to Know Magento 2 Clear Cache Command Line

The functionality of cache could be visualized to that part of a memory brain, which is relatively not ample. Cache is of engine factors accelerating loading speed of a Magento 2 site, but it could reverse its-so-called-duty by clogging the flow if database exceedingly accumulates in one small place. In Magento 2 cache system, there are more than 12 typical kinds of cache to store various data – see cache overview here.

Any Magento 2 users must have been familiar with a repeatedly yellow hung up notification bar said “one or more of the Cache types are invalidated” found after any adjustment was made for your site. As long as you are with Magento 2, it seems to continually ask to refresh cache types in order to liberate cache to execute your commands.

Flush Cache notification
Flush Cache notification

The reason Magento endlessly asks you to clear cache is because if it was not cleared on a regular frequency, the likely chance of causing a jam or miscarry your commands would happen. This article’s purposes are to guide you how to use command line to clean cache without the support of developers; furthermore, to compile all possible ways to help you address that matter in Magento 2 CE – a task you should always perform as long as you are with Magento 2.

How to clear cache using Command line?

Commonly, cache can either be cleaned manually from using Command Line; the other way is to clear manually in Admin Panel. There are two more ways to clean them programmatically or to create extension automatically refreshing cache using cron in Magento 2, which may require more technical knowledge and consultant from developers.

1. Manually clear cache using Command line in Magento 2

– To enable/disable cache, enter:

bin/magento cache:enable [type] ... [type]

bin/magento cache:disable [type] ... [type]

Omit type to enable/ disable all caches. The type option is a space-separated list of cache types.

php bin/magento cache:clean [type] ... [type]

php bin/magento cache:flush [type] ... [type]

The type option is a space-separated list of cache types.

-To flush all cache types, enter:

php bin/magento cache:flush

For example, to disable the full page cache and the Entity attribute value (EAV):

php bin/magento cache:disable eav full_page

-To check cache status, enter:

php bin/magento cache:status

– To clean static file cache, applying those steps:

Clear  pub/static and var/view_preprocessed directories and subdirectories except for pub/static/.htaccess.

-To clear the pub/static directory of all files except .htaccess (which is a hidden file), enter the following command:

rm -R pub/static/*

 

2. Manually clean cache from Admin Panel

2.1. How to clean certain cache

Login to Admin Panel, find System and choose cache management

Admin Panel > Cache Management
Admin Panel > Cache Management

You will see the cache grid management table. It is essential to define which cache is going to be pursed. If the targeted cache is disabled, you need to enable them on the ground that disabled cache types are not cleaned.

13 kinds of cache are disabled
13 kinds of cache are disabled

How to manipulate:

  • Select Cache Type, in the drop-down menu, choose Enable then Submit.
  • After Enable, once again in the drop-down menu choose Refresh then Submit.

This action does not interrupt other processes or applications because it cleans only the cache that Magento uses only.

Action: Refresh Cache
Action: Refresh Cache

2.2.  How to flush Magento cache in Admin Panel

In case you follow above guides precisely, but your site remains incapable of processing or resolving issues, you should flush cache to delete all storage cache. This action might affect some processes applications sharing one storage (for example other websites).

You will see this row below Cache Management

Flush Cache buttons
Flush Cache buttons

Hit Flush Magento Cache to purge all default Magento cache with associated Magento tag.

Hit Flush Cache Storage to purge all items of Cache regardless of its tag.

 

There is also additional Cache Management under cache grid table to clean.

Additional cache management
Additional cache management

Hit Flush Catalog Images Cache to clear away the catalog images from all sections; for example:

media/catalog/product/cache.

Hit Flush JavaScript//CSS Cache to discard merged copy of JavaScript – CSS files from the cache.

Hit Flush Statics Files Cache to remove preprocessed view files and static files.

Refresh your site after each step to check accomplishment.

3. Programmatically clean cache  

3.1 Define constructor

public function __construct(

    Context $context,

    \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,

    \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool

) {

    parent::__construct($context);

    $this->_cacheTypeList = $cacheTypeList;

    $this->_cacheFrontendPool = $cacheFrontendPool;

}

    

 3.2   Define cache you want to delete/ flush

   $types = array('config','layout','block_html','collections','reflection','db_ddl','eav','config_integration','config_integration_api','full_page','translate','config_webservice');

foreach ($types as $type) {

    $this->_cacheTypeList->cleanType($type);

}

foreach ($this->_cacheFrontendPool as $cacheFrontend) {

    $cacheFrontend->getBackend()->clean();

}

 

4. Get extension for Automatically Refresh Cache using Cron

See extension for auto flush cache using Cron in this link: https://github.com/kiutisuperking/FlushCache