0

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
  • You should use cache tags to selectively invalidate cache upon data changes. Commented Oct 31, 2023 at 13:12

2 Answers 2

0

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();
    }
}
0

You can flush cache using Magento 2 cron. References: https://devhooks.in/blog/automatically-refresh-cache-using-cron-magento-2

But automatically refresh cache using cron is not recommended way because in some cases maybe its cause issue in website performance if you constantly edit the content on your website. You can use this at your own risk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.