Deleting Magento Session Files

How you can go about deleting your Magento session files

Whilst being an excellent ecommerce platform, Magento isn't the tidiest of beasts and likes to leave session data lying around at its arse.

Session files are tiny so you could be forgiven for ignoring them most of the time, but the issue is there is a lot of them and Magento doesn't have a built in method for dealing with them.

Because they are so small but there is so much of them I have actually found times when a linux box has ran out of space but reports that there is still some space left, this I think is because the processes that calculate filesizes are falling over the sheer amount of session files left by Magento.

Up until recently I was incorrectly deleting the session files using a cronjob, but as was pointed out to me on Reddit this could have negative side effects for users of the site.

The correct way of handling this is to set up PHP to garbage collect, do this by adding the following to your php.ini file;

; The probability is calculated by using gc_probability/gc_divisor,
; e.g. 1/100 means there is a 1% chance that the GC process starts
; on each request.

session.gc_probability = 1
session.gc_divisor = 100

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
session.gc_maxlifetime = 1440

What I was doing before is something like;

find /path/to/magento/sessions/ -name "sess*" -type f -delete

But as I say, this isn't the preferred way to handle it.

Recent posts View all

Ruby Rails

Removing fields with a Rails migration

Ruby on Rails gives you powerful tools to make removing old fields easy, lets walk through how we can do this


Calculating Instagram Velocity From Your Browser

A small script that lets you quickly work out the velocity for posts within a hashtag