ruby-on-railsruby-on-rails-3

How to clear rails sessions table


I use active record store for rails sessions store.

Over just a short time, the size of sessions table has increased a lot. How are these session rows dumped after a certain period of time. Or should I manually clear them once in 24 hours?


Solution

  • A good blog post about your issue : http://blog.brightbox.co.uk/posts/clearing-out-rails-sessions

    The solution is to create a custom rake task:

    task :clear_expired_sessions => :environment do
        sql = 'DELETE FROM sessions WHERE updated_at < DATE_SUB(NOW(), INTERVAL 1 DAY);'
        ActiveRecord::Base.connection.execute(sql)
    end
    

    ... and run it every day with a cron job.