Blog

How to clear cache programmatically in Magento 2

In case of development, a developer or a request from merchant, he/she needs to clear/flush cache programmically. If you are having issue with clearing cache programmatically in Magento2, this article is for you. Today, we will show you how to Clear Cache Programmically.

You can flush cache programmatically in your controller, So don’t need to clear cache manually and don’t need to disable cache for particular block.

1.Add below lines after namespace

use Magento\Framework\App\PageCache\Version;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Cache\Frontend\Pool;

2.Define variables

protected $cacheTypeList;
protected $cacheFrontendPool;
  1. Add params to construct method
/**
 * Cache clear
 *
 * @param TypeListInterface $cacheTypeList
 * @param Pool $cacheFrontendPool
 */
public function __construct(
    TypeListInterface $cacheTypeList, 
    Pool $cacheFrontendPool
) {
    $this->cacheTypeList = $cacheTypeList;
    $this->cacheFrontendPool = $cacheFrontendPool;
}
  1. The Last step for clearing the cache in your execute method
    $_types = [
        'config',
        'block_html',
        'full_page',
        'config_webservice'
    ];
 
    foreach ($_types as $type) {
        $this->cacheTypeList->cleanType($type);
    }
   /* flushed the Entire cache storage from system, 
    Works like Flush Cache Storage button click on System -> Cache Management 
   */
    foreach ($this->cacheFrontendPool as $cacheFrontend) {
        $cacheFrontend->getBackend()->clean();
    }

Example: 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.

After you’ve done all the necessary things, remember to double-check the result to make sure everything is done right. I hope this tutorial is helpful for you. You can freely share it with your Magento friends to help them out. Thanks for reading and please stay tuned for our next useful instructions.

Leave a Reply

Your email address will not be published. Required fields are marked *