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?

  • You should use cache tags to selectively invalidate cache upon data changes. Commented Oct 31, 2023 at 13:12

2 Answers 2


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

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 = [
    foreach ($_types as $type) {
    foreach ($this->cacheFrontendPool as $cacheFrontend) {

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.