Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear a flask session?

While importing flask, we import modules such as session etc.

SecureCookieSession is a kind of dictionary, that can be accessed using session.

Now, I try to clear all the junk variables that I used while trying to build a website.

One of the answers on stackoverflow used a command like session.clear() for clearing the contents of a session. But such a command gives an error that no such command exists.

Can anyone point out for me how to clear the SecureCookieSession and how to clear the session every time I shutdown the server or close the website?

like image 594
user4413310 Avatar asked Jan 02 '15 19:01

user4413310


People also ask

How do you clear a session variable in Python?

To remove a session variable, use the pop() method on the session object and mention the variable to be removed.

How long does a session last in Flask?

Its good practice to time out logged in session after specific time, you can achieve that with Flask-Login. Default session lifetime is 31 days, user need to specify the login refresh view in case of timeout.

Where is Flask session stored?

Flask – Sessions A session with each client is assigned a Session ID. The Session data is stored on top of cookies and the server signs them cryptographically. For this encryption, a Flask application needs a defined SECRET_KEY.


2 Answers

from flask import session

session.clear()

I use session like this with flask, it does work. I don't use SecureCookieSession though, but maybe it can help.

like image 78
Jerry Unkhaptay Avatar answered Oct 18 '22 00:10

Jerry Unkhaptay


You can also iterate through the session and call session.pop() for each key in your session. Pop will remove the variable from the session and you don't have to keep updating your secret key.

for key in list(session.keys()):
     session.pop(key)
like image 28
TheF1rstPancake Avatar answered Oct 18 '22 02:10

TheF1rstPancake