15 days money back guarantee   Onetime payment, lifetime usage

Blog

Magento clear cache programatically

How to clear the Magento cache from PHP code? Below you will find two simple scripts which might help you to do this.

With these scripts you can easily clear your Magento cache programatically from code. You can call the code anywhere you want, set it up in cronjob or use it in a custom extension or something. The first code snippet is cleaning / emptying the Magento cache programatically. However, the second snippet will actually refresh the cache types. (Just like in Magento backend you can choose to clean / empty cache or to refresh all cache types).

Clean cache snippet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
      //increase execution time
      ini_set('max_execution_time', 900); //900 seconds = 15 minutes
 
      //require Magento
      require_once 'app/Mage.php';
      $app = Mage::app('admin');
      umask(0);
 
      //enable Error Reporting
      error_reporting(E_ALL & ~E_NOTICE);
 
      try {
            //CLEAN OVERALL CACHE
            flush();
            Mage::app()->cleanCache();
            // CLEAN IMAGE CACHE
            flush();
            Mage::getModel('catalog/product_image')->clearCache();
            //print
            print 'done';
      }
      catch(Exception $e) {
            //something went wrong...
            print($e->getMessage());
      }
?>
<?php
	//increase execution time
	ini_set('max_execution_time', 900); //900 seconds = 15 minutes

	//require Magento
	require_once 'app/Mage.php';
	$app = Mage::app('admin');
	umask(0);

	//enable Error Reporting
	error_reporting(E_ALL & ~E_NOTICE);

	try {
		//CLEAN OVERALL CACHE
		flush();
		Mage::app()->cleanCache();
		// CLEAN IMAGE CACHE
		flush();
		Mage::getModel('catalog/product_image')->clearCache();
		//print
		print 'done';
	}
	catch(Exception $e) {
		//something went wrong...
		print($e->getMessage());
	}
?>

Refresh cache snippet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
      //increase execution time
      ini_set('max_execution_time', 900); //900 seconds = 15 minutes
 
      //require Magento
      require_once 'app/Mage.php';
      $app = Mage::app('admin');
      umask(0);
 
      //enable Error Reporting
      error_reporting(E_ALL & ~E_NOTICE);
 
      $types=array('config','layout','block_html','translate','collections','eav','config_api','config_api2');
      foreach($types as $type) {
            $c = Mage::app()->getCacheInstance()->cleanType($type);
            Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => $type));
      }
?>
<?php
	//increase execution time
	ini_set('max_execution_time', 900); //900 seconds = 15 minutes

	//require Magento
	require_once 'app/Mage.php';
	$app = Mage::app('admin');
	umask(0);

	//enable Error Reporting
	error_reporting(E_ALL & ~E_NOTICE);

	$types=array('config','layout','block_html','translate','collections','eav','config_api','config_api2');
	foreach($types as $type) {
		$c = Mage::app()->getCacheInstance()->cleanType($type);
		Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => $type));
	}
?>

3 Responses to “Magento clear cache programatically”

  1. Which file I put ur program. Thanks

    • Emvee Solutions 07/13/2015 at 9:42 am

      Hi,
      You can just create a new file, for example ‘clearcache.php’ and put it in the root of your server! Then you go to your website.nl/clearcache.php and the script executes. Hope this helps.

  2. Thanks, working fine.

Leave a Reply to Emvee Solutions Click here to cancel reply.