How To Programmatically Empty Cache In Magento 2?

  • author-img Nidhi Arora
  • 6 years
How To Programmatically Empty Cache In Magento 2?

In some situations, you may feel a need to empty your Magento cache programmatically. That’s why we have come up with another “How to” blog to help you.

Read on to learn how to programmatically empty cache in Magento 2.

Step 1: First off, you need to define constructor – pass Magento\Framework\App\Cache\TypeListInterface and Magento\Framework\App\Cache\Frontend\Pool to your file’s constructor as explained below:

public function __construct(

 

Context $context,

 

\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,

 

\Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool

 

) {

 

parent::__construct($context);

 

$this->_cacheTypeList = $cacheTypeList;

 

$this->_cacheFrontendPool = $cacheFrontendPool;

 

}


Step 2:  So the second step is to add the code mentioned below. You can add this code to the method where you need to empty the cache.

$types = array(‘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();

 

}


And that’s it. Hope it helps you to effortlessly clear cache in Magento 2. In a case, if you feel this “how to” is like a heavy lifting for you, we at Envision Ecommerce have a robust team of certified Magento developers who is always ready to perform these kinds of heavy lifting for you. Contact us at https://www.envisionecommerce.com/ to perform this “How to” for you.

Download Blog

ENQUIRY

Ready to Get Started

Communication is the key for us to understand each other. Allow us to understand
your requirements or queries. Present us with an opportunity to serve you.

Fill out the form and out team will get back to you
within 24 hours

    Head Office

    815 Brazos St STE 500, Austin,
    TX 78701, USA