How would I clear all rails sessions?

In Rails 4, the accepted answer (rake db:sessions:clear) no longer works because ActiveRecord::SessionStore was extracted into the active_record-session_store gem. (See here for further explanation)

You can now either install the active_record-session_store gem and use rake db:sessions:clear as in Rails 3, or create a custom Rake task that looks like so:

namespace :db do
    namespace :sessions do
        desc "Clear ActiveRecord sessions"
        task :clear => :environment do
            sql = 'TRUNCATE sessions;'
            ActiveRecord::Base.connection.execute(sql)
        end
    end
end

If you are storing your sessions in a db then

rake db:sessions:clear

Whether it's DB or cookie store, use rake tmp:sessions:clear


Firstly, nil is not == false, however, nil evaluates to false. Try it yourself if you do not believe:

irb(main):001:0> nil == false
=> false
irb(main):002:0> nil == nil
=> true

Which ofcourse means:

irb(main):003:0> false.nil?
=> false

You can clean up your code in the following manner as it seems like @firsttime is never set to true anywhere.

unless session[:visited]
  session[:visited] = true
  initglobals
end

Finally, rake tmp:sessions:clear will only work if you are using ActiveRecordStore, if you are using CookieStore (which is the default). Then you will need to clean your cookies, or use reset_session.