Magento 2

How to clean and flush cache programmatically in magento2

Magento Store Performance & speed of your store plays a vital role to serve a great shopping experience to your shoppers. Also, its important ranking factor that keeps you stand out from the competition. Adding more products & data to your store simultaneously increases the possibility of making your store slower to load. Magento cache management is a handy tool to flush the cache and release storage by removing unnecessary catalog image cache and the JavaScript as well as CSS cache.

Magento offers several types of caching as per need. But after a modifying code or making changes that might affect HTML output on the store frontend, it’s highly recommended to keep Magento cache enabled. Working with Magento extension development, many times you need to flush cache programmatically to apply drastic changes on the store frontend. Recently, while developing an extension, we have implemented programmatically cache to get quick effects on store data. Today I would likely to share that code with you guys.

All you need to do is simply need to add the following code to your extension file.

use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Cache\Frontend\Pool;

public function __construct(TypeListInterface $typeListInterface,Pool $pool)
{
 $this->typeListInterface = $typeListInterface;
 $this->pool = $pool;
}

public function cachePrograme()
{

 $_cacheTypeList = $this->typeListInterface;

 $_cacheFrontendPool = $this->pool;

 $types = array('full_page');

 foreach ($types as $type) 
 {
  $_cacheTypeList->cleanType($type);
 }
 foreach ($_cacheFrontendPool as $cacheFrontend) 
 {
 $cacheFrontend->getBackend()->clean();
 }
}

Voila, now you successfully implemented programmatically cache. You can manipulate this code according to your requirements of caching.

Do comment if you are facing any issue while using this code.

Happy Caching!

Click to rate this post!
[Total: 19 Average: 4.5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago