# How to delete all retained messages from MQTT subtopic?

**URL:** https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200
**Category:** General
**Created:** [16 March 2020 20:32 UTC](https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200 "2020-03-16T20:32:05Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![techraf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/techraf/32/17389_2.png) [@techraf](https://discourse.nodered.org/u/techraf)
#### Post date: [16 March 2020 20:32 UTC](https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200/1 "2020-03-16T20:32:05Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/213569d83a79d49e6567a393a57151e1cff1b384.png)

```auto
[{"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](https://discourse.nodered.org/t/is-there-a-way-to-force-mqtt-nodes-to-re-subscribe-from-the-flow/23051), there's no way to achieve it.

Is there any other way?

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [16 March 2020 20:49 UTC](https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200/2 "2020-03-16T20:49:51Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![techraf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/techraf/32/17389_2.png) [@techraf](https://discourse.nodered.org/u/techraf)
#### Post date: [16 March 2020 21:16 UTC](https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200/3 "2020-03-16T21:16:35Z")

</div>

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

> [@bakman2](#):
>
> Do you want to have the retain flag on []

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.

> [@bakman2](#):
>
> [] and just a single excercise to delete them all ?

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

> [@bakman2](#):
>
> With a change node you can change content per topic.

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).

> [@bakman2](#):
>
> But isn't easier to just remove all topics from the broker with mqtt explorer for example ?

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.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [16 March 2020 21:34 UTC](https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200/4 "2020-03-16T21:34:02Z")

</div>

Hi @techraf

> [@techraf](#):
>
> 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.

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.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [16 March 2020 21:46 UTC](https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200/5 "2020-03-16T21:46:40Z")

</div>

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

---

<div class="post-metadata">

### Author: ![techraf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/techraf/32/17389_2.png) [@techraf](https://discourse.nodered.org/u/techraf)
#### Post date: [17 March 2020 10:28 UTC](https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200/6 "2020-03-17T10:28:46Z")

</div>

> [@Colin](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![techraf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/techraf/32/17389_2.png) [@techraf](https://discourse.nodered.org/u/techraf)
#### Post date: [17 March 2020 10:29 UTC](https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200/7 "2020-03-17T10:29:12Z")

</div>

> [@knolleary](#):
>
> And as you've said, there's no way to trigger a resubscribe with the MQTT nodes.

~~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](https://flows.nodered.org/node/node-red-contrib-digitaloak-mqtt) node.~~

It worked as described above, but it seems it was a combination of misconfiguration and a bug. Started [another topic](https://discourse.nodered.org/t/publishing-to-a-wildcard-mqtt-topic-disconnects-all-mqtt-nodes/23244) specifically about that behaviour.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [17 March 2020 10:55 UTC](https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200/8 "2020-03-17T10:55:57Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [17 March 2020 12:58 UTC](https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200/9 "2020-03-17T12:58:07Z")

</div>

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...

---

<div class="post-metadata">

### Author: ![techraf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/techraf/32/17389_2.png) [@techraf](https://discourse.nodered.org/u/techraf)
#### Post date: [17 March 2020 19:45 UTC](https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200/10 "2020-03-17T19:45:26Z")

</div>

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

> [@knolleary](#):
>
> One option might be to track the retained topics in your flow as they arrive on the MQTT In node.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [18 March 2020 08:44 UTC](https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200/11 "2020-03-18T08:44:44Z")

</div>

Here is a simple function node that does this

```auto
[{"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":""}]

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [1 April 2020 08:45 UTC](https://discourse.nodered.org/t/how-to-delete-all-retained-messages-from-mqtt-subtopic/23200/12 "2020-04-01T08:45:00Z")

</div>

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