I've written a module that stores data in a separate table. Because the data needs to be in a table (for a performant SQL JOIN) and should only be valid momentarily, the table uses the session ID as a field.

Now, as the table grows with time, we need some kind of cleanup. However, as the table contains data that is still valid, as well as data that is invalid on the account of being associated with expired sessions, we need to clean only the data from invalid and/or expired sessions.

The basic question is: How can we iterate over the currently active sessions? We would then clean all data associated with sessions that are not part of the collection of active sessions, maybe with some kind of cron job.

Another possibility I would prefer is to clean the data whenever a session becomes invalid. Is there an event or any other way to run code when a session becomes invalid?

1 Answer 1


You can put sessions in MySQL and use your session ID field as a foreign key with ON DELETE CASCADE. The core_session should already have garbage collection which makes this automatic for you, otherwise crontab this

  • Depends on what type of environment your running. For high performance webshops you rather have your sessions in something like memcache than the db. So please take into account OP. The moment you use memcache or want to start using it, this implementation would break.
    – Tim Hofman
    Commented Apr 12, 2014 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.