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
Leave a comment
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)
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.