Deleting 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


Quickly create gifs from movs in the command line

How to quickly take your .mov files and turn them into .gif


Removing unused Trix Editor buttons

How to quickly remove unused Trix Editor buttons