Clear Cache Programmatically Magento 2
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.
Enjoyed the tutorial? Spread it to your friends!
Featured Extensions

One Step Checkout
$199
190 reviews

Layered Navigation
$99
90 reviews

Automatic Related Products
$199
30 reviews

PDF Invoice
$199
24 reviews

Affiliate
$99
28 reviews

Reward Points
$99
22 reviews

CRO
$645

Product Feed
$149
33 reviews