How to Disable Caches programmatically in magento

By

April 4, 2012MagentoNo comments

Magento has an in built Caching System that improves the Performance of the website. But this feature should be turned off during Development and theme design to view our changes properly.

Sometimes we may come across a situation like our admin panel cannot be accessed due to some unfinished code or error. Even though we fixed the code magento may cache that error page and produce the same result. Even if we cleared the contents in the cache directory it will be filled again if the cache is enabled in admin. In Such cases we can programmatically disable and flush the cache.

The Below Code snippet disables the cache in code.

<?php

$mageFilename = 'app/Mage.php';

require_once $mageFilename;

umask(0);
Mage::app('admin');

Mage::app()->cleanAllSessions();
Mage::app()->getCacheInstance()->flush();
Mage::app()->cleanCache();

$types = Array(
          0 => 'config', 
          1 => 'layout',
          2 => 'block_html', 
          3 => 'translate', 
          4 => 'collections',
          5 => 'eav',
          6 => 'config_api',
          7 => 'fullpage'
        );

 $allTypes = Mage::app()->useCache();

$updatedTypes = 0;
foreach ($types as $code) {
   
    if (!empty($allTypes[$code])) {
         
        $allTypes[$code] = 0;
        $updatedTypes++;
        
    }
    $tags = Mage::app()->getCacheInstance()->cleanType($code);
}
if ($updatedTypes > 0) {
    Mage::app()->saveUseCache($allTypes);
    echo "Caches disabled Programmatically";
}
else {
    echo "Caches disabled Already";
}

Compatability: Magneto 1.4 onwards