Breaking News
Home / Web / Ruby / How to clean Rails Sessions?

How to clean Rails Sessions?

Clean Rails Sessions

By default rails does not clear out stale sessions from the session store. If Depending on the configuration session can be stored in the local file system or the database.

Method 1: (Applies to File Store)
Delete sessions say over ten hours old every four hours.
Otherwise your /tmp will end up overflowing.
This only applies you use the file store method of session storage with Rails.
1 */4 * * * find /tmp/ -name “ruby_sess*” -cmin +600 -exec rm {} ;

By default the session files are stored in /tmp. To change the location where the session files are created add the following to application.rb

ActionController::Base.session_options[:tmpdir] = “/path/to/session/folder/”

Method2 : (Application to both File or Database Store)

class SessionCleaner
def self.remove_stale_sessions
destroy_all( [‘updated_on <?’, 20.minutes.ago] )

And then invoke the remove_stale_sessions method every let say every 15 minutes via;

*/15 * * * * ruby /full/path/to/script/runner -e production “SessionCleaner.remove_stale_sessions”[pb_builder]

Leave a Reply