Alan Alan - 1 year ago 58
Python Question

Django does session.flush() expire the database session entry?

According to the Django documentation


Deletes the current session data from the session and deletes the session cookie. (django.contrib.auth.logout() function calls it)

But does it also automatically expire the session entry inside the database table?

Because if it doesnt, I think that might cause an unnecessary build up of non-usable but non-expired sessions inside the table.

Answer Source

Yes, it does. If you look at the source the docstrings explicitly state that:

def flush(self):
    Removes the current session data from the database and regenerates the
    self._session_key = None

That being said, there will be a build up of expired sessions. That's why you should set up a scheduled job to clear them up using clearsessions management command:

Django does not provide automatic purging of expired sessions. Therefore, it’s your job to purge expired sessions on a regular basis. Django provides a clean-up management command for this purpose: clearsessions. It’s recommended to call this command on a regular basis, for example as a daily cron job.