Skip to content

Commit

Permalink
Add management command to clear sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
jayqi committed Jan 1, 2025
1 parent 65ef2a9 commit 351f252
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions huntsite/management/commands/clear_sessions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.conf import settings
from django.contrib.sessions.models import Session
from django.core.cache import cache
from django.core.management.base import BaseCommand


class Command(BaseCommand):
help = "Force logout all users by clearing sessions"

def handle(self, *args, **kwargs):
# Clear database sessions
self.stderr.write("Clearing all sessions.")
Session.objects.all().delete()
self.stderr.write(self.style.SUCCESS("All sessions cleared."))
if "cached_db" in settings.SESSION_ENGINE:
# Clear cached sessions
self.stderr.write("Using cached_db session engine. Clearing cache.")
cache.clear()
self.stderr.write(self.style.SUCCESS("Cache is cleared."))

0 comments on commit 351f252

Please sign in to comment.