[Fixed]-Django does session.flush() expire the database session entry?

1👍

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
    key.
    """
    self.clear()
    self.delete()
    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.

👤Selcuk

Leave a comment