Delete a Session Variable

A snippet showing how to delete session variables in Django.


# views.py

def delete_session(request):
    try:
        del request.session['username']
    except KeyError:
        pass
    return HttpResponse("Session variable deleted")
      
Explanation:
  • Use del request.session['key'] to delete a session variable.
  • Removes the 'username' session variable.
  • Category Sessions & Cookies
  • Total Views 629
  • Last Modified 19 June, 2026
  • Tags #sessions #delete #request #django
Previous snippet
Set a Session Variable
Next snippet
Set Cookie in Response
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.