How to Clear Cache For Specific CMS Page Programmatically in Magento 2

How to Clear Cache For Specific CMS Page Programmatically in Magento 2

Hello Magento Folks?,

How are you all working? I hope all are safe and productive at home. Today I am with another interesting solution for How to Clear Cache For Specific CMS Page Programmatically in Magento 2. Meanwhile, If you have not visited the lately published article then have a look at MAGENTO 2: How to Change Default Active Tab on Admin Order View Page. So let’s begin?.

Introduction:

Basically, Magento uses much memory when it works and requires to flush the memory several times in a day. Many times Magento Developer requires flush cache for specific cms page because of the changes on cms pages or call another phtml file from cms page or a change in this phtml file. So directly after that you need to flush cache and for that flushing, the whole site cache is not appropriate because it will result in the downtime of the site. Therefore, make a practice to flush only specific cms pages and flush not affect the whole site. 

Step to Flush Cache:

For that, we have to create a PHP script which you need to put on the root in your installed Magento folder and you can directly run on a web browser.

<?php
use Magento\Framework\AppInterface;

try {
    require __DIR__ . '/app/bootstrap.php';

} catch (\Exception $e) {
    echo 'Autoload error: ' . $e->getMessage();
    exit(1);
}

try {
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $objectManager->get('Magento\Framework\App\State')->setAreaCode('frontend');
    $fullPageCache = $objectManager->get(\Magento\PageCache\Model\Cache\Type::class);
    $fullPageCache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array('cms_p_2')); // Here 2 is a cms page id here you can pass any cms page id
}
catch(\Exception $e){
    echo "Error :";
    echo $e->getMessage();
}

Hence, by applying the above you will be capable of flushing the specific CMS page successfully.

Final Words:

I hope the above solution to flush the specific CMS page was helpful for you. If you like the article then don’t forget to share with your Magento Friends and also comment down your reviews in the comment section below regarding the above solution. If you find any difficulties then do contact our Support Team for more guidance. Till then Stay Home Stay Productive.

Happy Coding.

Quick Cache and Reindex

Previous Article

Top 10 Ways to Reduce Shopping Cart Abandonment for Magento Store

Next Article

Ecommerce Affiliate Marketing: The Definitive Guide- 2024

Write a Comment
  1. hi Dhiren;
    thanks for posting this post .
    I was wondering how can I modify the code to clear cache only for a specific page like the landing page

Leave a Comment

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

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨