How to delete all retained messages from MQTT subtopic?

Is there a way to remove all retained messages under a subtopic from (i.e. using) Red-NODE?

[{"id":"891248f8.835ea8","type":"mqtt out","z":"fc382e1f.ee08d","name":"","topic":"","qos":"2","retain":"true","broker":"70574e00.5b08e4","x":450,"y":100,"wires":[]},{"id":"60c15933.a78008","type":"inject","z":"fc382e1f.ee08d","name":"","topic":"system/ticket/001","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":60,"wires":[["891248f8.835ea8"]]},{"id":"d3415875.513938","type":"inject","z":"fc382e1f.ee08d","name":"","topic":"system/ticket/002","payload":"2","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":100,"wires":[["891248f8.835ea8"]]},{"id":"ca7642f4.21be4","type":"inject","z":"fc382e1f.ee08d","name":"","topic":"system/ticket/003","payload":"3","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":140,"wires":[["891248f8.835ea8"]]},{"id":"5d70f289.32584c","type":"mqtt in","z":"fc382e1f.ee08d","name":"","topic":"system/ticket/+","qos":"2","datatype":"auto","broker":"70574e00.5b08e4","x":140,"y":200,"wires":[["d35f6088.dfd76"]]},{"id":"d35f6088.dfd76","type":"debug","z":"fc382e1f.ee08d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":470,"y":200,"wires":[]},{"id":"70574e00.5b08e4","type":"mqtt-broker","z":"","name":"localhost MQTT","broker":"localhosta","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Topics under system/ticket are UUIDs.

If I restarted flows and subsequently mqtt in node would re-subscribe, I would get retained messages from system/ticket/+ and could reset the payload for each topic, but as I already know from this topic, there's no way to achieve it.

Is there any other way?

This beautiful tool is called "Node-RED".

Do you want to have the retain flag on and just a single excercise to delete them all ?
With a change node you can change content per topic.

But isn't easier to just remove all topics from the broker with mqtt explorer for example ?

1 Like

I'm sorry, but I don't understand your reply.

To my knowledge there is no other way to delete a retained message, but to publish to that topic an empty message with retain flag being set.

I don't understand how could I want the flag to be on or off.

Yes, I want to delete them all, as I indicated above.

Yes. I know that. I wrote above I could do it if I got all retained messages and the problem was: I could get them only at the moment of mqtt in subscribing to the topic (for example, after restarting the flow).

It might be, but it is not what this question is about. By "Is there a way to [ ] from (i.e. using) Red-NODE" I meant how to delete the messages from within a flow.

Hi @techraf

That is right - you publish an empty to message with the retain flag set to true. But you do need to know exactly what topics have the retained messages on in order to publish to them.

So the question is how you can know what retained topics you need to delete. And as you've said, there's no way to trigger a resubscribe with the MQTT nodes. One option might be to track the retained topics in your flow as they arrive on the MQTT In node. Another would be to create a script outside of node-red that does connects a second client, does the subscribe and deletes - that script could then be run by an Exec node.

Neither option is entirely satisfactory.

1 Like

Can you explain why you need to do this so we can understand the problem better?

MQTT retained messages under system/ticket are job tickets processed and closed automatically, i.e. deleted from the queue after completion.

Unless something goes awry.

For such a case, at the simplest, I wanted to give an operator, using the dashboard, an emergency option to delete all active tickets with a click of a button.

Well, I said that, because I did not get an answer under the other topic, but now, I was actually able to force MQTT nodes to reconnect with _unsubscribe message.

Although not reliably enough yet, i.e. sometimes MQTT nodes enter the disconnect state permanently, sometimes they do reconnect.

I'm doing this by trial and error.

And after I force a reconnect, I can get retained MQTT messages into a Node-RED flow, even on request with the digitaloak-mqtt node.

It worked as described above, but it seems it was a combination of misconfiguration and a bug. Started another topic specifically about that behaviour.

I think this might be one of the few situations where I would recommend persistent context rather than retained MQTT. Publish to them not retained in MQTT and use the context for the persistent requirement.

1 Like

another possible option - depending on where the broker is running and permissions and is persistence turned off - would be to bounce the broker (stop and restart) - bit drastic...

1 Like

For now, this seems to me to be most appropriate:

Here is a simple function node that does this

[{"id":"53f62ce7.beb6a4","type":"inject","z":"3ead5d77.6a6672","name":"Clear","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":1380,"wires":[["dac46da.6d7fd9"]]},{"id":"467b1978.6ebb28","type":"mqtt in","z":"3ead5d77.6a6672","name":"","topic":"#","qos":"2","datatype":"auto","broker":"4132992e.4527c8","x":180,"y":1340,"wires":[["dac46da.6d7fd9"]]},{"id":"dac46da.6d7fd9","type":"function","z":"3ead5d77.6a6672","name":"","func":"var list = context.retainedlist || [];\n\nif (msg.hasOwnProperty(\"retain\")) { // it's from mqtt\n    if (msg.retain === true) { // add to list\n        list.push({topic:msg.topic,payload:\"\",retain:false});\n        context.retainedlist = list;\n    }\n    return null;\n}\nelse { // it must be the clear message\n    return [list];\n}\n","outputs":1,"noerr":0,"x":440,"y":1340,"wires":[["d4b89c4f.67e3a","69af253a.f18a2c"]]},{"id":"4132992e.4527c8","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.