Mohamed Abbas | Architect Magento | Tech Blogger | Magento Trainer

Mohamed Abbas
Mohamed Abbas
Architect Magento | Tech Blogger | Magento Trainer

What is the difference between Magento 2’s cache clean & flush?

Cache Types vs. Cache Backends in Magento 2

Magento has several cache types which can each utilize different cache backends. Cache backends are essentially the storage engines for the cache data and can be shared among multiple cache types. These backends are specified in the app/etc/env.php file.

For example:

 
				
					'cache' => [
    'frontend' => [
        'default' => [
            'id_prefix' => '69d_',
            'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
            'backend_options' => [
                'server' => 'redis',
                'database' => '0',
                'port' => '6379',
                'compress_data' => '1'
            ]
        ],
        'page_cache' => [
            'id_prefix' => '69d_',
            'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
            'backend_options' => [
                'server' => 'redis',
                'database' => '1',
                'port' => '6379',
                'compress_data' => '0'
            ]
        ]
    ]
]
				
			

In a typical setup, Magento uses two cache backends: default (for general cache types) and page_cache (for full-page caching). Though both backends use Redis here, each backend has its own Redis database (e.g., database 0 for default and database 1 for page_cache). This separation allows you to target and clear specific cache data effectively.

Cache Commands: cache:clean vs. cache:flush

For complete cache clearing across all backends, use:

				
					bin/magento cache:flush

				
			

This command clears all cached data in all backends, including tags for all cache types. However, if you only need to clear a specific cache type, it’s better to use:

				
					bin/magento cache:clean

				
			

This only removes tags for specified cache types without purging an entire backend, making it less resource-intensive.

Important: Avoid combining specific cache types with cache:flush:

				
					bin/magento cache:flush layout full_page

				
			

Since cache:flush works on the backend level, specifying cache types here can lead to unexpected behavior. This is why it may be beneficial for Magento to introduce a new command, cache:purge, to clear specific cache backends directly.

Cache Buttons in the Admin Panel

The admin panel also has some confusing cache buttons:

 

Flush Cache Storage: Similar to cache:flush, it clears all backend data.

Flush Magento Cache: Functions like cache:clean and might be better named “Clean Magento Cache” to match its purpose.

This distinction between clean and flush commands, along with the admin buttons, can be confusing for many users, so a more intuitive naming convention and possibly a new backend-specific purge command could improve clarity.

Picture of Mohamed Abbas

Mohamed Abbas

Technical Lead | Magento Architect