Cleaning the cache by TAG is quite easy in Magento 2. You will need to:

  • Inject the classes in your construct function
  • Know the exact tags you want to cache
  • Flush the actual Cache

Start creating your class injecting the classes we need:

use Magento\Backend\App\Action\Context;
use Magento\Framework\App\CacheInterface;
class YourClass extends AnyClass
     * @var CacheInterface
    protected $cacheManager;    

    public function __construct(
        Context $context,
        CacheInterface $cacheManager
    ) {
        $this->cacheManager = $cacheManager;

Create a function to retrieve the tags you want to flush. For example, for a CMS page with an ID 2:

     * Return cache tags to flush
     * @return array
    public function getCacheTags()
        return [\Magento\Cms\Model\Page::CACHE_TAG . '_2'];

And, in a custom function you can just execute the cache cleaning with:


Important note: As I have experienced myself, if you use Varnish, this command won’t flush the varnish cache for that page. I’ve created the following question in StackExchange to see if someone can help.

Categories: Development


Leave a Reply

Avatar placeholder

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.