If you have the ability to run a python script, then I have a tool that you can use to purge the retained messages on topics. You’ll need to install the paho-mqtt
python library to do this. (Doing something like pip install paho-mqtt
.) Here’s a script: https://www.transsys.com/stuff/watch.py and you might edit the beginning to change the default MQTT broker address and stuff.
Of course, you can run this from anywhere you have a python environment that can connect to your MQTT broker.
So you should be able to do something like
python watch.py --broker 192.168.0.42 --retained AuroraA4/#
and see messages published to that (wildcard) topic. (The #
is a wildcard match for anything that follows.) Normally, the script won’t print any messages with the retained flag, unless you pass that --retained
option. So you should see the messages with with (retained)
right after the topic name immediately after the script connects to the broker.
The retained flag indicates that you received it because it was previously published and waiting for you. If another message is published with the retain
flag set, current subscribers won’t see that indication delivered to them.
Anyway.
To purge those messages, kill/interrupt the script and run it again with the --purge-retained
option added and it will delete the retained messages it receives when the script subscribes to the topics on the broker.
Here’s an example, with the topic foo
on my MQTT broker.
louie@kzin[84] $ ./watch.py --retained foo
[2021-04-21 23:21:17] foo(retained): bar
^C
louie@kzin[85] $ ./watch.py --purge-retained --retained foo
[Purging retained message on 'foo']
[2021-04-21 23:24:12] foo(retained): bar
[2021-04-21 23:24:12] foo:
^C
louie@kzin[86] $ ./watch.py --purge-retained --retained foo
^C
louie@kzin[87] $
The first invocation subscribes to the foo
topic and shows there’s a retained message present.
Then the next with the purge option and an indication that each message it discovers is purged. You’ll also see the null message that was published to the topic.
Finally, I kill that and run it again to show the retained message is gone.
I normally use this script just to watch MQTT traffic on my broker; you can subscribe to multiple topics (or topic patterns), dump it out in JSON and stuff. I just hacked in this purge thing over the last 15 minutes, so it’s a quick-and-dirty solution. Add the --help
option and maybe that will answer more questions than it creates on how to use it.