Jun 22, 2014
kalpesh

Magento: Clear all caches from command line

Magento clear all caches from command line, programatically from ssh. Clearing the caches is a must when you are making any configuration changes in your Magento website. Although you can always clear the cache from admin panel, sometimes for faster cleaning or unable to log into admin panel reason, it’s good to have a script which will clear all the caches in Magento.

Create a file in your Magento root and name it clearCache.php with the below code:

<?php
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"]) ? "Cache cleared!" : "There is some error!";
        echo "\n";
    }
} catch (exception $e) {
    die("[ERROR:" . $e->getMessage() . "]");
}

echo "\n";

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

Make sure all the double quotes comes good in copy pasting.

You can now run this script by the command “php -f clearCache.php” from your magento root location in terminal and this will start clearing all the caches for you! Once done, it will confirm by the message “Cache cleared!” or giving error message if it fails.

4 Comments

  • Recommend using gmdate() and ” instead of “” for your strings.

  • Does this clear even /var/cache ??

    Cause when i run i get Cache cleared ! message, but the folder under var/cache remains still…

  • Please provide script for Enable Cache.

  • While configuring php shell script, We need to take care of these commands, Abstract.php, Compiler.php, Indexer.php, Log.php, You can check more details about these commands at, https://www.cloudways.com/blog/php-shell-scripts-magento/. Hope it will help your readers as well as I got help from your and this post.

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions