[Answered ]-Delete pieces of Session on browser close

2đź‘Ť

âś…

You’re confusing things a bit.

The only thing stored inside “Django’s session cookie” is an ID. That ID refers to the data which is stored inside the session backend: this is usually a database table, but could be a file or cache location depending on your Django configuration.

Now the only time that data is updated is when it is modified by Django. You can’t expire data automatically, except by either the cookie itself expiring (in which case the entire set of data persists in the session store, but is no longer associated with the client) or by running a process on the server that modifies sessions programmatically.

There’s no way of telling from the server end when a user leaves a website or closes his browser. So the only way of managing this would be to run a cron job on your server that gets sessions that were last modified (say) two hours ago, and iterate through them deleting the data you want to remove.

Leave a comment