Magento clear cache programmatically

In some cases, you may need to refresh Magento cache programmatically. For example, you have a cron script to update your store daily and changes will be affected only if cache is refresh. Here is a small snippet for you to refresh all cache types.

try {
    $cacheTypes = Mage::app()->useCache();
    foreach ($cacheTypes as $type => $option) {
        Mage::app()->getCacheInstance()->cleanType($type);
    }
} catch (Exception $e) {
    Mage::log($e->getMessage());
}

If you just need to refresh one type, Block HTML Output for instance, you just need to use this:

$type = 'block_html';
Mage::app()->getCacheInstance()->cleanType($type);
Comments