---
title: "How to clean and flush cache programmatically in magento2"
url: "https://magecomp.com/blog/how-to-clean-and-flush-cache-programmatically-in-magento2/"
date: "2018-04-05T19:27:32+00:00"
modified: "2025-02-22T08:54:56+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "Magento 2"
word_count: 274
reading_time: "2 min read"
summary: "Magento Store Performance &amp; speed of your store plays a vital role to serve a great shopping experience to your shoppers. Also, its important ranking factor that keeps you stand out from the co..."
description: "While developing a Magento extension, After a modifying code or making changes that might affect HTML output on the store frontend, we need to do caching. Bu..."
keywords: "Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Call Function from CLI Command in Magento 2?"
    url: "https://magecomp.com/blog/call-function-from-cli-command-magento-2/"
  - title: "How to Display Product Image in Backend Order Detail Page of Magento 2"
    url: "https://magecomp.com/blog/display-product-image-backend-order-detail-page-magento-2/"
  - title: "Magento 2: Truncate Table from Database using Data Patch Interface"
    url: "https://magecomp.com/blog/magento-2-truncate-table-from-database-using-data-patch-interface/"
---

# How to clean and flush cache programmatically in magento2

_Published: April 5, 2018_  
_Author: Dhiren Vasoya_  

![programmatically Caching M2](https://magecomp.com/blog/wp-content/uploads/2018/04/programmatically-Caching-M2-1024x332.webp)

Magento Store Performance & speed of your store plays a vital role to serve a great shopping experience to your shoppers. Also, its important ranking factor that keeps you stand out from the competition. Adding more products & data to your store simultaneously increases the possibility of making your store slower to load. Magento cache management is a handy tool to flush the cache and release storage by removing unnecessary catalog image cache and the JavaScript as well as CSS cache.

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-2.webp)](https://magecomp.com/magento-performance-optimization.html)

Magento offers several types of caching as per need. But after a modifying code or making changes that might affect HTML output on the store frontend, it’s highly recommended to keep Magento cache enabled. Working with Magento extension development, many times you need to flush cache programmatically to apply drastic changes on the store frontend. Recently, while developing an extension, we have implemented programmatically cache to get quick effects on store data. Today I would likely to share that code with you guys.

[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-2.webp)](https://magecomp.com/services/hyva-theme-development/)

All you need to do is simply need to add the following code to your extension file.

```
use MagentoFrameworkAppCacheTypeListInterface;
use MagentoFrameworkAppCacheFrontendPool;

public function __construct(TypeListInterface $typeListInterface,Pool $pool)
{
	$this->typeListInterface = $typeListInterface;
	$this->pool = $pool;
}

public function cachePrograme()
{

	$_cacheTypeList = $this->typeListInterface;

	$_cacheFrontendPool = $this->pool;

	$types = array('full_page');

	foreach ($types as $type)
	{
		$_cacheTypeList->cleanType($type);
	}
	foreach ($_cacheFrontendPool as $cacheFrontend)
	{
	$cacheFrontend->getBackend()->clean();
	}
}
```

[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-2.webp)](https://magecomp.com/services/hire-magento-programmer/)

Voila, now you successfully implemented programmatically cache. You can manipulate this code according to your requirements of caching.

Do comment if you are facing any issue while using this code.

Happy Caching!

[![Quick Cache and Reindex](https://magecomp.com/blog/wp-content/uploads/2020/05/Quick-Cache-and-Reindex.png)](https://magecomp.com/magento-2-quick-cache-and-reindex.html?utm_source=blog&utm_medium=CTA&utm_campaign=cacheandreindex)


---

_View the original post at: [https://magecomp.com/blog/how-to-clean-and-flush-cache-programmatically-in-magento2/](https://magecomp.com/blog/how-to-clean-and-flush-cache-programmatically-in-magento2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-07-22 09:23:23 UTC_  
