# Session destroy callback?

**URL:** <https://www.ruby-forum.com/t/session-destroy-callback/77425>\
**Category:** Rails\
**Created:** [November 27, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/session-destroy-callback/77425 "2006-11-27T22:54:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 27, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/session-destroy-callback/77425/1 "2006-11-27T22:54:41Z")

</div>

All.

Is there a method that we can define/override that is guaranteed to be  
called whenever a session is destroyed (regardless of how it is  
destroyed)?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 28, 2006, 9:14am UTC](https://www.ruby-forum.com/t/session-destroy-callback/77425/2 "2006-11-28T09:14:31Z")

</div>

Wes G. wrote:

> All.
> 
> Is there a method that we can define/override that is guaranteed to be  
> called whenever a session is destroyed (regardless of how it is  
> destroyed)?
> 
> Thanks,  
> Wes

Sort of, but not really. If you are using ActiveRecordStore you can add  
a before\_destroy method to your Session model, but you have to delete  
the entries with a session\_obj.destroy call. If you have a cronjob that  
simply runs a

DELETE FROM sessions WHERE updated\_at \< (_some\_time\_ago_)

Then there is no callback, because rails never gets invoked at all.

The problem is that most methods of session cleanup happen without the  
rails app even knowing it happened at all.
