How to manage Magento 2 Cache using CLI commands

Magento 2 employs backend caching at many levels within the system -- File System (default), Database, Redis and Varnish, which is used to store web pages to speed up the load processes and improve your Magento 2 site performance.

In this post, we are going to introduce a couple of handy CLI (Command Line Interface) commands -- one of the simplest methods to manage your Magento 2 cache. We will also make a short overview of Magento 2’s default cache backend solution -- file system; the difference between cache:flush vs. cache:clean.

Let’s roll in.

Magento 2 Cache types at a glance

Magento 2 does a lot of caching. By default, these caches are all located in Magento 2’s root var/cache/ and /var/page_cache/ directories. The cache record will look like:

Magento 2 cache records

Sample Magento 2 cache records

These record file names are composed of the prefix mage— followed by an Id (a unique string) that identifies that particular record, combined with a tag (a way to identify the type of cache record) and some other unique identifiers like the first 2 characters of the md5 hash of the path to the file. This pattern makes each record unique and allows you to clean all cache records tagged with a given tag (or tags).

When opening the System | (Tools) Cache Management in your Magento 2 admin panel, you will see the following grid on the screen:

Magento 2 cache types

Magento 2 Cache Management

As indicated in the screenshot, Magento 2 has twelves cache types. These are:

 Cache type Tags Description When to clean this cache type?
Configuration config Various XML configurations that were collected across module and merged After making changes to configuration files or settings via the admin panel
Layout layout_general_cache_tag Layout building instructions After modifying layout files
Block HTML output block_html Page blocks HTML After modifying the view layer
Collections Data collection_data Collection data files Magento cleans up this cache automatically. But it also depends on your specific use case or custom module logic.
Reflection Data reflection API interfaces reflection data Removes a dependency between the Webapi module and the Customer module.
Database DDL operations db_ddl Results of ddl queries, such as describing tables or indexes Magento cleans up this cache automatically. But it also depends on your specific use case or custom changes to your database schema.
EAV types and attributes eav Entity types declaration cache You should not typically need to clean or flush this cache type.
Integrations Configuration integration Integration configuration file After changing or adding integrations
Integrations API Configuration integration_api_config Integrations API configuration file
Page Cache fpc Full page caching After modifying code level that affects HTML output
Translations translate Translation files Merged translations from all modules
Web Services Configuration webservice REST and SOAP configurations, generated WSDL file After modifying configuration files

Overview of Magento 2 cache types

How to manage Magento 2 Caches using CLI commands

In this section, we will talk more about cache management. If for any reason you do not want to use the Magento 2 cache management dashboard, you can manage cache from the built-in bin/magento console tool that allows for fast and simple cache management.

Working faster with bin/magento

This is a handy entry point to working with all Magento 2’s CLI commands. When you run command in terminal: php -f bin/magento, you will get the list of Magento 2 command line available, it will show like this:

Magento 2 cache records

The built-in bin/magento console tool

This reference lists all available Magento 2 cache commands that you can use as needed.

How to check the cache status

To get the current state information of the cache, open your Terminal window, go to Magento 2 directory and run command: php bin/magento cache:status

If all caches are enabled, it should look like this sample:

Magento 2 cache status

Check cache status

How to Disable Cache Types
You can execute the command on terminal: php bin/magento cache:disable to disable all cache types.

A sample after running the command:

Disable Magento 2 cache

Disable cache status

If you want to disable a specific cache type, you can execute the command: php bin/magento cache:disable CACHE_TYPE

For more information on the CACHE_TYPE, you can refer to the Tags column in the Overview of Magento 2 cache types table afore-mentioned in this post.

An example to disable Full Page Cache:

Disable Magento 2 cache

Disable full page cache

How to Enable Cache types
Similar to disabling cache types, open your terminal and run one of the following commands in your Magento 2 root:

php bin/magento cache:enable
php bin/magento cache:enable CACHE_TYPE

An example to enable all cache types:

Enable Magento 2 cache

Enable Magento 2 cache

Clean Cache vs. Flush Cache types

Right now there are two options of deleting caches supported in Magento 2:
php bin/magento cache:flush
php bin/magento cache:clean

Let’s see what is the difference between these two methods.

Magento 2 cache

Clean cache vs. Flush Cache types

The cache:clean method, as the “Flush Magento Cache” button in Magento 2 admin does, tries to delete all items from enabled Magento 2 cache types only, according to their associated Magento tags in the default Magento 2 var/cache and /var/page_cache/ directories, including:

  • Magento\Framework\App\Cache\Type\Config
  • Magento\Framework\App\Cache\Type\Layout
  • Magento\Framework\App\Cache\Type\Block
  • Magento\Framework\App\Cache\Type\Collection
  • Magento\Framework\App\Cache\Type\Reflection
  • Magento\Framework\DB\Adapter\DdlCache
  • Magento\Eav\Model\Cache\Type
  • Magento\Customer\Model\Cache\Type\Notification
  • Magento\Integration\Model\Cache\Type
  • Magento\Integration\Model\Cache\TypeIntegration
  • Magento\PageCache\Model\Cache\Type
  • Magento\Framework\App\Cache\Type\Translate
  • Magento\Webapi\Model\Cache\Type\Webapi

That means if you use the alternative cache types (memcached etc.,), those will not be invalidated.

In other words, this option does not affect other processes or applications because it cleans only the cache that Magento 2 uses. Disabled cache types are not cleaned as well.

The cache:flush method, as the “Flush Cache Storage” button in Magento 2 admin does, will remove entire cache entries, regardless of tags. Once you flush a cache type, it purges the cache storage, which might affect other processes applications that are using the same storage.

So you should use the cache:flush with cautions, especially you are using one of the shared storage cache types -- for instance two different applications using the same memcached instance.

A practical tip is to use cache:flush type if you’ve already tried cleaning the cache and you’re still having issues that you cannot isolate.

Conclusion

We hope this post helps you get an overview on how to work with cache in Magento 2. Understanding all nuances related to cache:clean vs. cache:flush is vitally important when you work with Magento 2 extensions and themes.

Written By

Comments