# Cleaning up Session (Cookie Based)

**URL:** <https://www.ruby-forum.com/t/cleaning-up-session-cookie-based/132328>\
**Category:** Rails\
**Created:** [March 19, 2008, 6:39am UTC](https://www.ruby-forum.com/t/cleaning-up-session-cookie-based/132328 "2008-03-19T06:39:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharkie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sharkie](https://www.ruby-forum.com/u/sharkie)\
**Post date:** [March 19, 2008, 6:39am UTC](https://www.ruby-forum.com/t/cleaning-up-session-cookie-based/132328/1 "2008-03-19T06:39:22Z")

</div>

I am using the default Cookie-Based Session in Rails 2.0.

I have accumulated the bad practice of adding:

session[‘seen\_announcement\_’ + Date.today.to\_s] = true

over the years.

No users have complained yet, but I am assuming that each user must have  
hundreds of seen\_announcement\_XXXXX in their cookies

I would like to write a script to clean their sessions of those keys.  
Yet, I am finding the following:

1. session is not Hash. I cannot use session.keys to list all the keys  
in session. How would I do this? I would like to dump out a session then  
sort through the keys and delete the proper ones.

2. session.delete actually deletes the entire session. There is no  
method for deleting each key in a session.

Please help me on out this.

On the other hand, how does this work. Do all session variables get  
submitted along with HTTP request each time a user loads a page? So this  
means the more I store in session the slower a request takes?

---

<div class="post-metadata">

**Author:** ![sharkie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sharkie](https://www.ruby-forum.com/u/sharkie)\
**Post date:** [March 19, 2008, 8:49am UTC](https://www.ruby-forum.com/t/cleaning-up-session-cookie-based/132328/2 "2008-03-19T08:49:08Z")

</div>

On 19 Mar 2008, at 05:39, Sharkie L. wrote:

> have  
> 2) session.delete actually deletes the entire session. There is no  
> method for deleting each key in a session.
> 
> Please help me on out this.
> 
> On the other hand, how does this work. Do all session variables get  
> submitted along with HTTP request each time a user loads a page? So  
> this  
> means the more I store in session the slower a request takes?

With the cookie store, yes. Each response and request. Furthermore, if  
your cookie goes over maximum recommended sizes (i forget exactly how  
much) the browser is free to just ignore it.

Fred

---

<div class="post-metadata">

**Author:** ![sharkie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sharkie](https://www.ruby-forum.com/u/sharkie)\
**Post date:** [March 19, 2008, 10:04am UTC](https://www.ruby-forum.com/t/cleaning-up-session-cookie-based/132328/3 "2008-03-19T10:04:43Z")

</div>

So now that I have put a lot of variables in a user’s cookie, how can I  
go about removing them?

---

<div class="post-metadata">

**Author:** ![sharkie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sharkie](https://www.ruby-forum.com/u/sharkie)\
**Post date:** [March 20, 2008, 10:24am UTC](https://www.ruby-forum.com/t/cleaning-up-session-cookie-based/132328/4 "2008-03-20T10:24:51Z")

</div>

Sharkie L. wrote:

> So now that I have put a lot of variables in a user’s cookie, how can I  
> go about removing them?

I will even settle for the process of cleaning up an entire session  
removing up its data from cookies and starting from stcratch – doing  
this from Rails and not by requiring my users to delete cookies from  
browser.

---

<div class="post-metadata">

**Author:** ![sharkie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sharkie](https://www.ruby-forum.com/u/sharkie)\
**Post date:** [March 20, 2008, 11:09am UTC](https://www.ruby-forum.com/t/cleaning-up-session-cookie-based/132328/5 "2008-03-20T11:09:55Z")

</div>

On 20 Mar 2008, at 09:24, Sharkie L. wrote:

> Sharkie L. wrote:
> 
> > So now that I have put a lot of variables in a user’s cookie, how  
> > can I  
> > go about removing them?
> 
> I will even settle for the process of cleaning up an entire session  
> removing up its data from cookies and starting from stcratch – doing  
> this from Rails and not by requiring my users to delete cookies from  
> browser.

If you change the secret used by the cookie store that will invalidate  
all the sessions (you will get a bunch of exceptions as rails will  
think the cookies were tampered with though).

Fred
