Like I mentioned in my question I am having a problem with cleaning/flushing cache on category/product pages.

I get some code, probably for some older version of Magento but didn't work.

Here it is:

    $cacheTag = \Magento\Catalog\Model\Category::CACHE_TAG . '_' . $catId;
    $category = $this->categoryRepository->get($catId, $this->storeManager->getStore()->getId());

    $this->cacheManager->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, [$cacheTag]);
    $this->eventManager->dispatch('clean_cache_by_tags', ['object' => $category]);

The same example I found here:


I check Manager class and method clean have only one parameter(argument) and this is an array of types. So I tried with one argument and I get one error: Notice: Undefined index: instance in /app/vendor/magento/framework/App/Cache/TypeList.php online 75

So is there any other way how to clean cache on the category or product page?

Tnx for the help.

2 Answers 2


You can use this code :

namespace Vendor\Extension\Block;

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 MyCustomcacheFunction()
       $_types = [
        foreach ($_types as $type) 

        foreach ($this->cacheFrontendPool as $cacheFrontend) 

Now call your Block function by creating the object of block class where you want to do the caching.

  • Thank you for response. This code will clean/flush all categories or just categories that I want? I need clean/flush just few of them (i am saving category_id in database table and then within foreach I am trying clean/flush cache with cache tag cat_c_id ). Commented Oct 28, 2021 at 5:57
  • then you can pass the category id on function call and check if you want to perform for that one or not. Commented Oct 28, 2021 at 6:08

The root cause of the issue is different id added in cache.xml & different tag is used in "TYPE_IDENTIFIER" constant in cache Instance file. Both value should be same to resolve the issue.

<type name="custom_cache" translate="label,description" instance="Md\CustomCache\Model\Cache\Type\CustomCache">
    <label>Custom Cache Example</label>
    <description>Ceated Custom Cache For Tutorial</description>

Instance File

 * Type Code for Cache. It should be unique
const TYPE_IDENTIFIER = 'custom_cache';

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.