Some times we need to clear cache programmatically.
You can do that by using the below code.
<?php
namespace MagemonkeysCacheBlock;
class CacheClear extends MagentoFrameworkViewElementTemplate
{
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoFrameworkAppCacheFrontendPool $cacheFrontendPool,
MagentoFrameworkAppCacheTypeListInterface $cacheTypeList,
array $data = []
) {
$this->_cacheFrontendPool = $cacheFrontendPool;
$this->_cacheTypeList = $cacheTypeList;
parent::__construct($context, $data);
}
public function cacheClear()
{
/* get all types of cache in system */
$allTypes = array_keys($this->_cacheTypeList->getTypes());
/* Clean cached data for specific cache type */
foreach ($allTypes 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();
}
}
}
Call function like below,
$clearCache = $block->cacheClear();

