Magento Performance Boost: Cleanup Old Session Files

Share

If left unchecked, Magento session files can grow out of control and have a negative impact on your sites performance (aswell as taking up storage space). This article explains how to make sure old session files are removed automatically.

Magento stores it’s session files in var/session and I’ve seen too many Magento installations where the size of this directory has grown into the gigabytes. A quick browse of this directory should tell you whether you have a problem or not.

PHP has it’s own ‘garbage collection’ process for deleting old session files, but it’s disabled in Debian “due to the strict permissions on /var/lib/php5″
So, if you are using Debian then you’ll need to setup a simple cron script to do this garbage collection for you.

For this example, let’s say the path to your Magento installation is /path/to/magento and we want to delete session files over a week old.

Create a new file /path/to/magento/session_cleanup.sh
vi /path/to/magento/session_cleanup.sh
Add the following 2 lines to the file
#!/bin/sh
find /path/to/magento/var/session -name ‘sess_*’ -type f -mtime +7 -exec rm {} \;
Make sure the file is executable
chmod u+x /path/to/magento/session_cleanup.sh
Now add it to your crontab
crontab -e
and set it to run every day, let’s say at 3am

0 3 * * * /bin/sh /path/to/magento/session_cleanup.sh

Leave a Reply

You must be logged in to post a comment.