I can't seem to find it anywhere... How do I delete/destroy/reset/empty/clear a user's session in Rails? Not just one value but the whole thing..
5 Answers
To clear the whole thing use the reset_session method in a controller.
reset_session
Here's the documentation on this method: http://api.rubyonrails.org/classes/ActionController/Base.html#M000668
Resets the session by clearing out all the objects stored within and initializing a new session object.
Good luck!

- 12,432
- 5
- 49
- 65
-
1For database-based sessions (which you should probably use) you can expire through a query: http://guides.rubyonrails.org/security.html#session-expiry – m33lky Feb 24 '12 at 07:14
session in rails is a hash object. Hence any function available for clearing hash will work with sessions.
session.clear
or if specific keys have to be destroyed:
session.delete(key)
Tested in rails 3.2
added
People have mentioned by session={}
is a bad idea. Regarding session.clear
, Lobati comments- It looks like you're probably better off using reset_session [than session.clear], as it does some other cleaning up beyond what session.clear does. Internally, reset_session calls session.destroy, which itself calls clear as well some other stuff.
-
Anybody have any thoughts on the merits of this method vs the one recommended by Gdeglin? – Peter Berg Jan 16 '14 at 17:00
-
3This can be used when you want to retain other parameters but delete one particular key value pair. – Lavixu Jan 17 '14 at 05:59
-
I suppose I was referring to the use of either `session.clear` or `session = {}` vs the use of `reset_session`. Are they identical? – Peter Berg Jan 17 '14 at 12:22
-
2This will not reset the session at all, it will assign a local variable. Never use this technique to reset the session: session = {} – alexspeller Feb 04 '14 at 13:38
-
4The [Rails documentation](http://guides.rubyonrails.org/action_controller_overview.html), on section _5.1 Accessing the Session_ recommends to use reset_session if you want remove script-inserted key/value pairs (ex: Something inserted from a controller) and generate a new session. If you want reset only the key/value pairs you set, set those keys to nil. – sargas Apr 24 '14 at 22:33
-
Downvote for same reason as @alexspeller mentions. `session = {}` does nothing but set an empty local variable, which creates an insidious bug in your application and still leaves the session floating around untouched. – lobati May 11 '16 at 18:55
-
@lobati What about the other thing the answerer suggested, session.clear Is that OK? What exactly is the difference between session.clear and the other answer of reset_session? – barlop Jul 03 '18 at 09:18
-
1@barlop It looks like you're probably better off using `reset_session`, as it does some other cleaning up beyond what `session.clear` does. Internally, [`reset_session` calls `session.destroy`](https://github.com/rails/rails/blob/afc2b424e27fe48f83d5d1be73a8fadc38499c0c/actionpack/lib/action_dispatch/http/request.rb#L350), which [itself calls `clear` as well some other stuff](https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/request/session.rb#L79-L87). – lobati Jul 03 '18 at 16:23
to delete a user's session
session.delete(:user_id)

- 833
- 16
- 42
-
-
4@Arthur That's not true. But you'll only delete the :user_id key from the session and not the whole session – Andión Mar 06 '18 at 12:05
To clear only certain parameters, you can use:
[:param1, :param2, :param3].each { |k| session.delete(k) }

- 4,333
- 2
- 43
- 58
add this code to your ApplicationController
def reset_session
@_request.reset_session
end
( Dont know why no one above just mention this code as it fixed my problem ) http://apidock.com/rails/ActionController/RackDelegation/reset_session

- 6,367
- 14
- 83
- 137

- 71
- 1
- 3
-
I find that `@_request.reset_session` and `reset_session` both work and maybe do the same thing? – barlop Jul 03 '18 at 09:09
-
Oh my god i was searching fot 2 weeks and dealing with this issue. Thank you so much for the support. – Carlos Morales Jun 06 '23 at 04:33