Removing Old Rails Session Files
I’m sure that this is elementary for all of the *nix experts out there but I recently discovered this and thought that I would share. Ruby on Rails, by default, holds session information on disk in [application root]/tmp/sessions/ and unfortunately they aren’t purged automatically. Even for small sites, the number if session files can quickly grow out of control. For large sites, it can bring an application server to its knees by filling up the disk.
The “best practice” for Rails sites is to use the database to keep track of sessions using following commands:
- ruby script/generate session_migration
- rake db:migrate
Then uncomment the line in the environment.rb file that reads: config.action_controller.session_store = :active_record_store
If you’ve completed the above steps, then your sessions should now be stored in the database but that doesn’t remove the session files from disk. If you’ve been running with sessions on disk for a while, the command, rm /[application root/tmp/sessions/ruby_sess.* will fail with the error “Argument list too long”. If this happens, you can delete the files by running the following command:
find /[application root]/tmp/sessions/ -name ‘ruby_sess.*’ | xargs rm
I found this little gem of information at ducea.com. The comments have other commands that you can run to delete mass files if necessary.