Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

Clear Cache Programmatically Magento 2

Vinh Jacker | 12-18-2024

Clear Cache Programmatically

In case of development, a developer or a request from merchant, he/she needs to clear/flush cache programmically. If you are having issue with clearing cache programmatically in Magentoe, this article is for you. Today, we will show you how to Clear Cache Programmically.

Before starting the instruction, let me remind you of some essential knowledge about Magento 2 cache clear.

Related Post:

Why should clear cache in Magento 2?

Magento 2 cache clean & cache flush: The differences

Magento 2 cache clean/clear and Magento 2 cache flush is not the same.

About Cache clean/clear: Magento 2 cache clean/clear is the action that deletes all enabled Magento-related caches. It doenn’t clear other parts of the server that are not related to Magento.

About Cache flush: Magento 2 cache flush is the action that cleans all cache storage. Unllike cache clean/clear, this action has an impact on other parts of the storage that is a part of the same storage. You flush cache in Magento 2 when the cache clean does not reflect the changes at the frontend after the new backend configuration.

It’s simple to differentiate these two action. Also, it’s important to remember because if you mistake these two action, it can cause unexpected changes in your store.

Now we will see how to clear/clean cache programmatically in Magento 2.

How to clear cache programmatically in Magento 2

Implement these lines of codes in Helper:

<?php
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 flushCache(Version $subject)
{
  $_types = [
            'config',
            'layout',
            'block_html',
            'collections',
            'reflection',
            'db_ddl',
            'eav',
            'config_integration',
            'config_integration_api',
            'full_page',
            'translate',
            'config_webservice'
            ];
 
    foreach ($_types as $type) {
        $this->cacheTypeList->cleanType($type);
    }
    foreach ($this->cacheFrontendPool as $cacheFrontend) {
        $cacheFrontend->getBackend()->clean();
    }
}

Now call flushCache() function in a controller or model.

Final words

After you’ve done all the necessary things, remember to double-check the result to make sure everything is done right. I hope this tutorial is helpful for you. You can freely share it with your Magento friends to help them out. Thanks for reading and please stay tuned for our next useful instructions.

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services