[Django] #37070: Add .clear_messages() method to django.contrib.messages

5 views
Skip to first unread message

Django

unread,
Apr 27, 2026, 5:42:49 AM (2 days ago) Apr 27
to django-...@googlegroups.com
#37070: Add .clear_messages() method to django.contrib.messages
----------------------------+--------------------------------------------
Reporter: benjaoming | Type: New feature
Status: new | Component: contrib.messages
Version: | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------------
Disclaimer: This is really a minor issue 😊

We can do:

```
from django.contrib.messages import get_messages
```

And then we can call `get_messages()` - and if we iterate over them, they
will be cleared.

But we can't explicitly clear them because this method doesn't exist:

```
from django.contrib.messages import clear_messages
```

For me, the use-case was test-related: Writing an e2e test case that calls
some views and at each step verified messages, then cleared them.

Not sure if adding the method would make people use the messages framework
wrongly, but I think it makes sense to note that ADDING `clear_messages`
to the mix might make some people think you need to call it... which in
most cases, you don't, and therefore the documentation needs to be
''clear'' (pun intended).

Forum discussion: https://forum.djangoproject.com/t/why-isnt-it-easier-to-
clear-messages/5727/7
--
Ticket URL: <https://code.djangoproject.com/ticket/37070>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 27, 2026, 7:51:35 AM (2 days ago) Apr 27
to django-...@googlegroups.com
#37070: Add .clear_messages() method to django.contrib.messages
----------------------------------+--------------------------------------
Reporter: benjaoming | Owner: (none)
Type: New feature | Status: new
Component: contrib.messages | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Sarah Boyce):

* version: => dev


Old description:

> Disclaimer: This is really a minor issue 😊
>
> We can do:
>
> ```
> from django.contrib.messages import get_messages
> ```
>
> And then we can call `get_messages()` - and if we iterate over them, they
> will be cleared.
>
> But we can't explicitly clear them because this method doesn't exist:
>
> ```
> from django.contrib.messages import clear_messages
> ```
>
> For me, the use-case was test-related: Writing an e2e test case that
> calls some views and at each step verified messages, then cleared them.
>
> Not sure if adding the method would make people use the messages
> framework wrongly, but I think it makes sense to note that ADDING
> `clear_messages` to the mix might make some people think you need to call
> it... which in most cases, you don't, and therefore the documentation
> needs to be ''clear'' (pun intended).
>
> Forum discussion: https://forum.djangoproject.com/t/why-isnt-it-easier-
> to-clear-messages/5727/7

New description:

Disclaimer: This is really a minor issue 😊

We can do:

{{{
from django.contrib.messages import get_messages
}}}

And then we can call `get_messages()` - and if we iterate over them, they
will be cleared.

But we can't explicitly clear them because this method doesn't exist:

{{{
from django.contrib.messages import clear_messages
}}}

For me, the use-case was test-related: Writing an e2e test case that calls
some views and at each step verified messages, then cleared them.

Not sure if adding the method would make people use the messages framework
wrongly, but I think it makes sense to note that ADDING `clear_messages`
to the mix might make some people think you need to call it... which in
most cases, you don't, and therefore the documentation needs to be
''clear'' (pun intended).

Forum discussion: https://forum.djangoproject.com/t/why-isnt-it-easier-to-
clear-messages/5727/7

--
--
Ticket URL: <https://code.djangoproject.com/ticket/37070#comment:1>

Django

unread,
Apr 27, 2026, 7:56:58 AM (2 days ago) Apr 27
to django-...@googlegroups.com
#37070: Add .clear_messages() method to django.contrib.messages
----------------------------------+------------------------------------
Reporter: benjaoming | Owner: (none)
Type: New feature | Status: new
Component: contrib.messages | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by Sarah Boyce):

* stage: Unreviewed => Accepted

Comment:

Accepting without requesting this be added to the new features board as
this is based off an existing older discussion with already a good
consensus that this is useful and a small addition
--
Ticket URL: <https://code.djangoproject.com/ticket/37070#comment:2>

Django

unread,
Apr 28, 2026, 10:56:46 AM (14 hours ago) Apr 28
to django-...@googlegroups.com
#37070: Add .clear_messages() method to django.contrib.messages
----------------------------------+------------------------------------
Reporter: benjaoming | Owner: (none)
Type: New feature | Status: new
Component: contrib.messages | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Comment (by Natalia Bidart):

My only request would be to name this consistently with how we reset the
queries:

{{{#!pycon
>>> from django.db import connection, reset_queries
>>> connection.queries
[]
>>> reset_queries()
>>>
}}}

Therefore I think the function should be called `reset_messages()`.
--
Ticket URL: <https://code.djangoproject.com/ticket/37070#comment:3>
Reply all
Reply to author
Forward
0 new messages