How to Flush, Enable, Disable Cache Command Line in Magento 2

How to Flush, Enable, Disable Cache Command Line in Magento 2
August 29, 2020 458 view(s)
How to Flush, Enable, Disable Cache Command Line in Magento 2

How to flush Cache Command Line in Magento 2 is a general topic when you guys using Magento store. Magento 2 has 12 cache types by default. There are 5 simple commands to manage cache in command line. In this post I will show you to conduct each command line step by step.

I will explain cache management in this screenshot

Magento 2 Flush Cache Command Line

Magento 2 How to Flush Cache Command Line

  • Go to Magento root directory

Flush Cache Storage

php bin/magento cache:clean 

A short command line:

php bin/magento c:c 

Flush Magento cache

php bin/magento cache:flush 

A short command line:

php bin/magento c:f 
  • Finish! Go to your Magento store and check the result.

How to change current directory in Ubuntu, Centos or Windows

  • Ubuntu: cd /var/www/magento2
  • CentOS: cd /var/www/html/magento2
  • Windows: cd /d/xampp/htdocs/magento2

In Windows case, I suppose that you should install Xampp in D drive.

In Magento 2, let try to show command line guide by php bin/magento, it will show like this:

 Magento CLI version 2 Usage: command [options] [arguments] Options: --help (-h) Display this help message --quiet (-q) Do not output any message --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug --version (-V) Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output --no-interaction (-n) Do not ask any interactive question Available commands: help Displays help for a command list Lists commands admin admin:user:create Creates an administrator admin:user:unlock Unlock Admin Account cache cache:clean Cleans cache type(s) cache:disable Disables cache type(s) cache:enable Enables cache type(s) cache:flush Flushes cache storage used by cache type(s) 

In this guide, I will talk more about Cache management in the command line.

How to check cache status

First of all, let show cache status by the following command line:

php bin/magento cache:status 

Result of cache status

Current status: config: 1 layout: 1 block_html: 1 collections: 1 reflection: 1 db_ddl: 1 eav: 1 config_integration: 1 config_integration_api: 1 full_page: 1 translate: 1 config_webservice: 1 

Clean Cache command line (SSH)

php bin/magento cache:clean 

Flush cache storage used by cache types(s) command line

The cache storage may contain additional data such as server cache.

php bin/magento cache:flush 

Disable Cache command line

The following command will disable all cache types

php bin/magento cache:disable 

If you would like to disable specific cache type, you should type command line

php bin/magento cache:disable CACHE_TYPE 

Example:

php bin/magento cache:disable config 

How to enable Cache command line

It is similar to how to Disable cache all types and specific cache type

Enable all cache types

php bin/magento cache:enable 

Enable specific cache type

php bin/magento cache:enable CACHE_TYPE 

Example

php bin/magento cache:enable layout 

How to Clear Cache Programmatically

In case of development, a developer or a request from merchant, he/she needs to clear / flush cache programmically. So today we will show you how to Clear Cache Programmically. Implement these lines of codes in Helper:

<?php
use Magento\Framework\App\PageCache\Version;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Cache\Frontend\Pool;
protected $cacheTypeList;
protected $cacheFrontendPool;
 
public function __construct(
    TypeListInterface $cacheTypeList, 
    Pool $cacheFrontendPool
){
    
    $this->cacheTypeList = $cacheTypeList;
    $this->cacheFrontendPool = $cacheFrontendPool;
 
}
 
public function flushCache(Version $subject)
{
  $_types = [
            '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();
    }
}

Now call flushCache() function in a controller or model.