I need to flush some types of cache (or maybe all of them) by Magento cron tasks (not system native), also I might need it to be done periodically, is there any way to do this?
1 Answer
You can add below command in cpanel cron
php bin/magento cache:flush
if you want to run perticular cache then you can define
php bin/magento cache:flush layout
or you can add below code in cron and create cron too
<?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)
{
//define type which you want to clear cache
$_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();
}
}