Caution: This post was published in 2019 and may contain information, techniques, or code examples that are no longer current. Please double-check official documentation and modern best practices before using anything from this article.
April 2019

Magento Code: Magento 2 Cache Refresh Script

This is a stand alone script which can be called to refresh the cache in Magento 2

<?php
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php'); // Update as required, this line will work if placed in the Magento Root Directory
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
?><div style='top:45%; position:absolute; width:75%; text-align:center; color:#666; font-weight:bold; font-size:1.8em; '><?php
try{
$_cacheTypeList = $objectManager->create('Magento\Framework\App\Cache\TypeListInterface');
$_cacheFrontendPool = $objectManager->create('Magento\Framework\App\Cache\Frontend\Pool');
$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) {
$_cacheTypeList->cleanType($type);
}
foreach ($_cacheFrontendPool as $cacheFrontend) {
$cacheFrontend->getBackend()->clean();
}
?><span style='color:green'>&#9679; Success. </span> The cache has been cleared. <?php
}catch(Exception $e){
?><span style='color:red'>&#9679; Error. </span><?php
echo $msg = 'Error : '.$e->getMessage();die();
}
view raw cache.php hosted with ❤ by GitHub

The script hooks into Bootstrap to generate a unique instance without needing to hook into Magento itself. The System will loop through all the cache types specified on line 13. If you have any custom modules that also require a cache, add it into this list.

More posts from Magento

Using WordPress as a static site generator

Static site generators have gained significant traction amongst developers, designers, and businesses seeking faster, more secure websites. Unlike traditional dynamic sites, which rely on a database to deliver content on…

Continue reading

Moving a WordPress Website with ACF and Custom Post Types to Brightspot CMS

Migrating a website from WordPress to Brightspot CMS can seem daunting, particularly when the WordPress installation relies heavily on Advanced Custom Fields and Custom Post Types . Both ACF amd…

Continue reading

What's going on between WordPress and WP Engine?

The disagreement between WordPress and WP Engine has sparked considerable debate within the WordPress community and could have important implications for users of the WordPress content management system (CMS). WP…

Continue reading

Combining Laravel with WordPress

Combining Laravel with WordPress offers a unique and powerful approach to web development, blending the strengths of both platforms to create highly efficient, flexible, and scalable applications. Laravel, a modern…

Continue reading

you