As stated in Jerry Unhaptai's answer , as well as in the relevant section of the Flask documentation , you can simply do:
from flask import session session.clear()
Although, as Alejandro rightly pointed out in a comment:
If you also use reprogrammed messages in your application, you should be aware that reprogrammed messages are stored in session and therefore can be deleted before they flash if you clear session .
My suggestion is to take advantage of list comprehension:
[session.pop(key) for key in list(session.keys())]
in fact, this is the same for loop as in TheF1rstPancake's answer , albeit with a single line . We can delete everything except flashed messages from session (or add add any other conditions, for that matter) quite easily, for example, like this:
[session.pop(key) for key in list(session.keys()) if key != '_flashes']
source share