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

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

3 hours ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

3 hours ago

8 Best Social Login Apps for Shopify Store in 2024

Running an eCommerce business can be incredibly demanding, leaving entrepreneurs little time to focus on…

3 hours ago

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

1 day ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago