Ruby on Rails and Postgres: how to clear out sessions

If you’re running Ruby on Rails with a PostgreSQL database then you can clear out old session data by executing this command from your application directory:

ruby script/runner -e production \
"ActiveRecord::Base.connection.delete(\" \
DELETE FROM sessions \
WHERE updated_at < timestamp 'now' - interval '24 hours'\")"

This removes any sessions older than one day.

You might want to run this on a regular basis, perhaps even automatically with a cron entry.

This is the equivalent of the MySQL command described in the great book Agile Web Development With Rails (2nd edition).

Tcsh users: you may have to prefix the above command with sh if you get an error message about Unmatched “..

|