Clear Cache Programmatically 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 Magentoe, this article is for you. Today, we will show you how to Clear Cache Programmically.
Before starting the instruction, let me remind you of some essential knowledge about Magento 2 cache clear.
Related Post:
- How to Add Product Attribute Programmatically in Magento 2?
- How to Create Invoice Programmatically in Magento 2?
- How to Create Customer Programmatically in Magento 2?
Why should clear cache in Magento 2?
Magento 2 cache clean & cache flush: The differences
Magento 2 cache clean/clear and Magento 2 cache flush is not the same.
About Cache clean/clear: Magento 2 cache clean/clear is the action that deletes all enabled Magento-related caches. It doenn’t clear other parts of the server that are not related to Magento.
About Cache flush: Magento 2 cache flush is the action that cleans all cache storage. Unllike cache clean/clear, this action has an impact on other parts of the storage that is a part of the same storage. You flush cache in Magento 2 when the cache clean does not reflect the changes at the frontend after the new backend configuration.
It’s simple to differentiate these two action. Also, it’s important to remember because if you mistake these two action, it can cause unexpected changes in your store.
Now we will see how to clear/clean cache programmatically in Magento 2.
How to clear cache programmatically in Magento 2
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.
Final words
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.
- How to create a simple Hello World module for Magento 2
- Magento 2 Block Template Ultimate Guides
- How to Create Module in Magento 2
- How to Create Controller in Magento 2
- How to create CRUD Models in Magento 2
- How to Create Magento 2 Block, Layout and Templates
- Configuration - System.xml
- How To Create Admin Menu In Magento 2
- Admin ACL
- Admin Grid