Des de la administració de Magento es pot esborra la cache, però en alguns casos, es fa necessari esborrar la cache de manera automàtica. El següent script permet automatitzar esborrar la cache de Magento des del cron de sistema.


date_default_timezone_set("Europe/Madrid");
echo "Start Cleaning all caches at ... " . date("Y-m-d H:i:s") . "\n\n";
ini_set("display_errors", 1);

require '../app/Mage.php';
Mage::app('admin')->setUseSessionInUrl(false);
Mage::getConfig()->init();

$types = Mage::app()->getCacheInstance()->getTypes();

try {
echo "Cleaning data cache... \n";
flush();
foreach ($types as $type => $data) {
echo "Removing $type ... ";
echo Mage::app()->getCacheInstance()->clean($data["tags"]) ? "[OK]" : "[ERROR]";
echo "\n";
}
} catch (exception $e) {
die("[ERROR:" . $e->getMessage() . "]");
}

echo "\n";

try {
echo "Cleaning stored cache... ";
flush();
echo Mage::app()->getCacheInstance()->clean() ? "[OK]" : "[ERROR]";
echo "\n\n";
} catch (exception $e) {
die("[ERROR:" . $e->getMessage() . "]");
}

try {
echo "Cleaning merged JS/CSS...";
flush();
Mage::getModel('core/design_package')->cleanMergedJsCss();
Mage::dispatchEvent('clean_media_cache_after');
echo "[OK]\n\n";
} catch (Exception $e) {
die("[ERROR:" . $e->getMessage() . "]");
}

try {
echo "Cleaning image cache... ";
flush();
echo Mage::getModel('catalog/product_image')->clearCache();
echo "[OK]\n";
} catch (exception $e) {
die("[ERROR:" . $e->getMessage() . "]");
}

Script original

Configurar l'script en el cron mitjançant crontab -e


01 08 * * * /cron_refresh_cache.php > /dev/null 2>&1