how to clear existing flash messages in Flask

Viewed 5999

I have a warning flash message in Flask that that appears before the user tries to submit a form based on background information about the user. If the user goes ahead and submits the form the way they were warned not to, they are prevented and see a second flash message. I'd like to clear the first flash message before the user sees the second.

I've read the Flask documentation on flash messages and tried to google for the answer. I also read some of the Flask source code. No solution jumps out at me.

Can anyone help me figure out how to clear a flash message?

2 Answers

This way you can clear the flash message as there is no predefined method to clear the flash message in Flask flash helpers. You can try the below code. It works for me and maybe useful to you.

session.pop('_flashes', None)

The flashes messages are stored in the session from flask, you can access them by typing.

session['_flashes']

This is a list with the messages stored on flash and it has a method clear() for, mm well yo know.

session['_flashes'].clear()

With that you can clean the stored messages on flash

Psdta: a little bit late but i just run in that issue and that is the way that i solved

Related