Clearing ActiveRecord-saved sessions

AWDwR book is full with wizdom. Will try to post here from time to time a small gems from it, suitable for our Rails production environment. For easier searching the title lines will be prefixed with [Agile]. So the first gem, something like an extension to the previous Storing sessions in your database post.

Sessions accumulate and are never removed. We need to do cleaning ourselves. To delete files, that haven't been touched in the last 12 hours, with the file-based session handler we can do (possible invoked by cron):

find /tmp/ -name 'ruby_sess*' -ctime +12h -delete

Interesting is however how to clean database saved sessions. Enter AWDwR:

cd myapp && RAILS_ENV=production ./script/runner \
'ActiveRecord::Base.connection.delete 
    "DELETE from sessions WHERE updated_at < now() - 12*3600"'