Clearing out old Rails session records from a MySQL database

Posted . Visible to the public.

To delete sessions based on a hard coded cut-off date:

DELETE FROM sessions WHERE updated_at < STR_TO_DATE('23.05.2013',GET_FORMAT(DATE,'EUR'));

Replacing cut-off date with period of inactivity

DELETE FROM sessions WHERE updated_at < DATE_SUB(CURDATE(), INTERVAL 30 DAY);

You can as well create a rake task and set up a cron job for automatic clearing

SomeModel.connection.execute 'DELETE FROM sessions WHERE updated_at < DATE_SUB(NOW(), INTERVAL 24 HOUR)'
Sandheep
Posted by Sandheep to Sandheep's deck (2013-05-10 18:03)