Pruning Magento Cache & Sessions Using Cron

Over the weekend a client contacted me because they had serious performance issues with their Magento based store. The three main issues were:

  1. Checkout taking more than 40 seconds to complete after clicking to submit payment. (This of course causes double orders/payments and users just leaving/lower sale conversions.).
  2. Login to the front or back end taking excessively long.
  3. Slow importing of new products that would even timeout.

Upon investigation I narrowed down the issues to Magento’s Cache. Sounds weird, I know, since “caching” should, in theory, be faster. However, Magento caches heavily and does not remove invalidated files. Also Magento had saved sessions that amounted to well over 3 million files!


Pruning Magento Cache and Sessions

Once I purged cache the issue was resolved. However, it takes less than 24 hours for the cache to again grow large enough that the issues would return. For me, completely deleting cache every few hours didn’t seem like a good solution because visitors who hit the site just after cache was completely purged would face a slower than usual experience.

Therefore, I implemented a balanced approach by deciding to setup an hourly cron script that would find and delete files older than X. With that approach, the issues above were resolved without any noticeable performance hit when cache is pruned, instead of purged.

The script I ended up using was:


find /var/www/html/prod/var/cache/ -type f -mmin +120 -exec rm {} \;
find /var/www/html/prod/var/session/ -type f -mmin +120 -exec rm {} \;
find /var/www/html/prod/var/report/ -mtime +7 -exec rm -f {} \;
find /var/www/html/prod/var/tmp/ -mtime +1 -exec rm -f {} \;

* Change your path of course and run as root. (test first by manually launching or using a single command).

This Prunes Magento’s cached files older than 2 hours, session files older than 2 hours, report files older than 7 days and tmp files older than 1 day. This now keeps the cache & session files below millions of files which become more of an issue with slower disk.

Make sure your Magento cookie (session) timeout is set shorter than the prune TTL. In this case, Magento’s cookie timeout is set to 1 hour so pruning files older than 2 hours won’t delete any active sessions! This server also uses Varnish FPC and APC for opcode and Magento caching.

Hope this helps another admin who experiences these issues. Please add your solution to this issue if different.

Update: Also see –