How To

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.

Click to rate this post!
[Total: 5 Average: 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

  • 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

Recent Posts

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 hours ago

6 Innovative Tools Revolutionizing E-Commerce Operations

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

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

3 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

3 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

5 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We have brought exciting news for Magento store owners. Hyvä Themes recently released 1.3.6 and…

5 days ago