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.
0c51049fb747cd60088e4878458e05d1?d=identicon&s=25 Rick (Guest)
on 2007-03-30 21: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.
Db31811eb76d98459bc9e6cb3f043f55?d=identicon&s=25 Dutch Rapley (Guest)
on 2007-03-30 21:32
(Received via mailing list)
I typically do

session[:the_var] = nil

someone please correct me if there's a better way.
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-03-30 21: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.