Sessions are stored in the database by default in Django. If you leave it alone, the number of expired sessions will increase, so I would like to show you how to delete it.
Django 3.0.5 Python 3.8.2
If you check the specifications, the recommended method is written. Clearing the session store
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 your job to clean up regularly, as it doesn't clean up automatically. But it feels like I have a command.
Apparently, there is a command called clearsessions.
Just go to the directory where manage.py is and type the following command.
$ python manage.py clearsessions
This seems to remove ** only expired sessions ** from the database.
You can see the session in the ** django_session ** table. Currently contains ** 2 ** data.
sqlite> select session_key, expire_date from django_session;
3dub24wutcq28y7lhgnfl2rasoy37646|2020-07-11 19:20:40
g7s29wnv45boguo5np33yroq61t4v9c2|2020-07-25 13:50:10
expire_date is 2020-07-11 and 2020-07-25.
Check the current date and delete the session.
$ date
Sunday, July 12, 2020 23:00:36
$ python manage.py clearsessions
Let's check the table.
sqlite> select session_key, expire_date from django_session;
g7s29wnv45boguo5np33yroq61t4v9c2|2020-07-25 13:50:10
There are only ** 1 ** cases. As of 2020-7-12 23:00:36, the 2020-07-11 session has expired and has been deleted.
Let's actually check the source code of the command. source of clearsessions
class SessionStore(SessionBase):
...
@classmethod
def clear_expired(cls):
cls.get_model_class().objects.filter(expire_date__lt=timezone.now()).delete()
expire_date < timezone.now()
You can see that the data before the current time is targeted.
https://stackoverflow.com/questions/7296159/django-session-database-table-cleanup
Recommended Posts