[PYTHON] How to delete expired sessions in Django

Delete expired sessions in Django: blush:

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.

Tried environment

Django 3.0.5 Python 3.8.2

Confirmation of specifications

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.

How to delete

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.

Verification

DB table check

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.

Delete by command

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.

For your information

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.

Referenced site

https://stackoverflow.com/questions/7296159/django-session-database-table-cleanup

Recommended Posts

How to delete expired sessions in Django
How to reflect CSS in Django
How to do Server-Sent Events in Django
How to convert DateTimeField format in Django
How to implement Rails helper-like functionality in Django
How to reflect ImageField in Django + Docker (pillow)
How to run some script regularly in Django
How to create a Rest Api in Django
How to get multiple model objects randomly in Django
How to use bootstrap in Django generic class view
How to upload files in Django generic class view
How to use Decorator in Django and how to make it
How to develop in Python
How to reference static files in a Django project
How to Delete with SQLAlchemy?
How to write custom validations in the Django REST Framework
How to use Laravel-like ORM / query builder Orator in Django
How to check ORM behavior in one file with django
How to update user information when logging in to Django RemoteUserMiddleware
[Django] How to give input values in advance with ModelForm
How to generate a query using the IN operator in Django
In Django, how to abbreviate the long displayed string as ....
[Python] How to do PCA in Python
How to handle session in SQLAlchemy
[Django] How to test Form [TDD]
How to write soberly in pandas
Errors related to memcached in django
How to update Spyder in Anaconda
How to use SQLite in Python
How to convert 0.5 to 1056964608 in one shot
How to create / delete symbolic links
How to get started with Django
How to kill processes in bulk
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to delete a Docker container
How to write Django1.9 environment-independent wsgi.py
How to run TensorFlow 1.0 code in 2.0
How to handle Japanese in Python
How to log in to Docker + NGINX
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to call PyTorch in Julia
[Django] How to read variables / constants defined in an external file
How to deploy a Django app on heroku in just 5 minutes
How to delete multiple specified positions (indexes) in a Python list
How to do arithmetic with Django template
How to use calculated columns in CASTable
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
How to check the version of Django
How to convert csv to tsv in CLI
[Itertools.permutations] How to put permutations in Python
How to use Google Test in C
How to implement nested serializer in drf-flex-fields
How to work with BigQuery in Python
How to execute commands in jupyter notebook
How to do'git fetch --tags' in GitPython