use auto flush cache on Magento

How to use auto flush cache on Magento 2?


The flush cache is a very special function of Magento 2. The cache is a particular area of your hosting server. It helps to increase the page load speed by storing the web pages through browsers. Also, it reduces resource requirements in the situation of heavy traffic.

Every time we edit any information or configuration content after saving we have to clear the cache in admin or run it by command via terminal.

Today, we are going to do an extension auto flush cache, so that after each saves is automatically clear the cache no longer needs to run commands on the terminal or in the admin.

The files need to be added.

Create an module, observer and events.xml.

  • Create Magepow\AutoFlushCache\registration.php
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
use Magento\Framework\Component\ComponentRegistrar;

  • Create Magepow\AutoFlushCache\module.xml
<?xml version="1.0"?>

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magepow_AutoFlushCache">
<module name="Magento_Backend"/>
<module name="Magento_Config"/>
  • Create Magepow\AutoFlushCache\Observer\FlushCache.php
namespace Magepow\AutoFlushCache\Observer;

class FlushCache implements \Magento\Framework\Event\ObserverInterface
public function __construct(
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
) {
$this->cacheTypeList = $cacheTypeList;
$this->cacheFrontendPool = $cacheFrontendPool;

public function execute(\Magento\Framework\Event\Observer $observer)
$types = array('config','layout','block_html','collections','reflection','db_ddl','eav','config_integration','config_integration_api','full_page','translate','config_webservice');
foreach ($types as $type) {
foreach ($this->cacheFrontendPool as $cacheFrontend) {

Final create file events.xml in etc.


<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_postdispatch_adminhtml_system_config_save">
<observer name="auto_flush_cache" instance="Magepow\AutoFlushCache\Observer\FlushCache"/>

Run the commands to install the module is we are using auto flush cache.

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush


Hope this article will help you in some way, You can see useful articles in the next articles.

Anything you need support from Magento 2 feels free to contact us at Alothemes and

Phone: (+84)865633728