Back to Top

Flush Cache by Tag in Magento 2

Updated 5 March 2024

Magento 2 have excellent cache mechanism. There are different types of cache, caches have id, there are tags to group caches and even Magento allows to create custom cache type. All of these makes a very good mechanism for caching. 

We can flush Magento cache by types from Admin panel and command line. But what if we want to clean the cache only partially. We can do it by cleaning cache by tag. 

Let’s suppose we make any changes in a particular product or category or let’s suppose we show some data on product page like in our Magento 2 Auction module we show the auction related data on the product page. And every times someone bid on the product the data gets changed but it will not reflect untill we flush or clean the cache. 

We can flush and clean the cache programmatically as in “How to clean and flush cache programmatically in magento2”  to reflect the changes but it will remove the other static content, other blocks which were not updated and all the pages from cache. So how do we clean the cache only for that particular product and only the product related container and not header and footer? Solution to this problem is cleaning cache by tag. 

So now let see how can we clean caches partially with tags,

Searching for an experienced
Magento 2 Company ?
Find out More
private $fullPageCache;

private function getCache()
    if (!$this->fullPageCache) {
        $this->fullPageCache = \Magento\Framework\App\ObjectManager::getInstance()->get(
    return $this->fullPageCache;

public function cleanByTags()
    $productId = 21; //Id of the Product whose Cache need to be cleaned 
    $tags = ['CAT_P_'.$productId];
    $this->getCache()->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, $tags);

Here we have cleaned a product page cache whose id is 21. Similarly you can clear s category page cache by using “CAT_C” as prefix. So it will be like CAT_C_3, where 3 is the category id. And in same way you can find tags for other pages to clean.

Thanks for reading blog. Feel free to comment.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


  • rajeshwari
    • Sanjay Chouhan (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home