Automatically Refresh Cache Using Cron in Magento 2

Automatically Refresh Cache Using Cron in Magento 2

In this tutorial, we will give you simple steps to create Automatically refresh cache using cron in Magento 2 Extension. Please follow bellow simple steps to create auto refresh cache extension In Magento 2.

Some times it may happen that we change the content/data of cms page,block or other configuration at that time some of the cache has been invalidate so website performance can be down. Now just refresh cache automatically by setting cron. We will create one extension for to refresh cache automatically.

Follow some simple steps to create Auto refresh cache extension for Magento 2.
Namespace:- Vendor Modulename : AutoRefreshCache

Step 1: Create registration.php into app/code/Vendor/AutoRefreshCache


Step 2: Create module.xml into app/code/Vendor/AutoRefreshCache/etc

<?xml version="1.0"?> 
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> 
    <module name="Vendor_AutoRefreshCache" setup_version="1.0.0" schema_version="0.0.1"> 

Step 3: Create crontab.xml into app/code/Vendor/AutoRefreshCache/etc

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd"> 
    <group id="default"> 
        <job name="vendor_autorefreshcache_cron" instance="Vendor\AutoRefreshCache\Model\Cron" method="myCronFunction"> 
            <schedule>*/2 * * * *</schedule> 

Setp 4: Create Cron.php into app/code/Vendor/AutoRefreshCache/Model

namespace Vendor\AutoRefreshCache\Model; 
use Magento\Backend\App\Action\Context; 
use Magento\Backend\App\Action; 
use Magento\Framework\App\Cache\Manager as CacheManager; 
use Magento\Framework\App\Cache\TypeListInterface as CacheTypeListInterface; 
class Cron 
    public function __construct(\Magento\Framework\Model\Context $context,\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,\Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool) 
        $this->_cacheTypeList = $cacheTypeList; 
        $this->_cacheFrontendPool = $cacheFrontendPool; 
    public function myCronFunction() 
        $cache_types = array('config','layout','block_html','collections','reflection','db_ddl','eav','config_integration','config_integration_api','full_page','translate','config_webservice'); 
        foreach ($cache_types as $type) { 
        foreach ($this->_cacheFrontendPool as $cache_clean_flush) { 

Step 5: Open Command line in folder root of magento and run both commands.

php bin/magento setup:upgrade --keep-generated

php bin/magento cache:flush

For more informations about Magento 2 Useful Commands List Click Here and read our another Post.

Step 6: To set cron on your server you need to run following commands.

crontab -e

and then type the follwoing cron cofiguration for your magento 2 , you can set cron schedule as per your requirement.

*/2 * * * * php /var/www/html/magento2/bin/magento cron:run 

Save this and exit

Bingo! we are done Now you just do not worry about cache as it will automatically refresh. So by this way you can clean cache automatically in Magento2.

I hope this Magento post helped you to create Automatically refresh cache extension in Magento 2. Bookmark it for your future reference. Do comment below if you have any other questions or doubts on this post.

P.S. Do share this post with your team.