Forum: Ruby on Rails How to clear a session variable?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Rick (Guest)
on 2007-03-30 23:02
(Received via mailing list)
I'm sorry if this is really dumb, but I can't find the answer.

How do I clear (or unset) a session variable?  Not the whole session,
just one item.

I tried treating the session as a hash, but
does not work.
Dutch Rapley (Guest)
on 2007-03-30 23:32
(Received via mailing list)
I typically do

session[:the_var] = nil

someone please correct me if there's a better way.
Jason R. (Guest)
on 2007-03-30 23:38
(Received via mailing list)
session[:key] = nil seems to be the accepted way of doing this.

session is not just a hash, and if you look at the source, #delete takes
parameters and actually clears the entire session.

A lot of people (including me) run into this, and I'm still not sure why
was done this way. What's wrong with making session.clear  (or
empty it out and keeping session.delete(:key) to work like a hash? The
current implementation definitely violates the Principle of Least

This topic is locked and can not be replied to.