How to clear ALL saved mqtt messages from mosquitto

I saw the syntax mosquitto_pub -h [server] -r -n -t [XYZ] to clear a single message. My problem is that device developers have posted a lot of garbage messages. I have a java / paho database that I would like to change to do this automatically as needed, but I cannot post a message with a null byte. I tried

client.publish(topic,null); 

but that didn't seem to work.

Any suggestions on how to remove everything in bulk.

+8
source share
6 answers

There are two options for this, using the paho client code, depending on which of the 2 publish methods you use.

 MqttMessage msg = new MqttMessage(new byte[0]); msg.setRetained(true); client.publish(topic, msg); 

or

 client.publish(topic, new byte[0],0,true); 

Another option is to stop the mosquito and delete the persistence file and restart

+4
source

Here's how to do it right with a shell script.

 #!/bin/sh echo "cleaning " $1 " :: usage: cleanmqtt <host>" mosquitto_sub -h $1 -t "#" -v | while read line; do mosquitto_pub -h $1 -t "${line% *}" -r -n; done 

Just put it in a file called something like

finally_a_working_way_to_remove_all_those_annoying_messages.sh

Then run

 sh finally_a_working_way_to_remove_all_those_annoying_messages.sh localhost 

You will have to press Ctrl + C after a while when it receives all messages. This decision is rather rude. You cannot specify what to delete or anything.

+11
source

This should work:

 client.publish(topic, new byte[]{}, 0, true); 

In addition, you might be interested in this script from Eclipse Paho Python to clear a given topic hierarchy. You might want to implement this behavior in Java, but it looks like you're looking for a one-time solution, so maybe just use a Python script :)

+1
source

I stopped the broker on the server and restarted it. All saved message has disappeared. :) If you can do this, this is the easiest way, I'm sure.

0
source

If you use Mosquitto MQTT Broker, turn off Saved Messages using the official method provided by Mosquitto.

First find the mosquitto.conf file

(In my Ubuntu / EC2 instance, it is stored in the / etc / mosquitto directory, I assume that the path to the mosquitto.conf file is / etc / mosquitto / mosquitto.conf)

Edit with your favorite text editor, my nano.

 sudo nano /etc/mosquitto/mosquitto.conf 

and in this file replace “constancy is false” instead of “constancy is true”

 persistence false 

Now save the file (if you use nano, press Ctrl + O and then type to save, Ctrl + X to exit)

Now restart the mosquito using the commands below

 sudo service mosquitto stop sudo service mosquitto start 

Note: If this configuration path does not exist in your case, find the configuration file using this command-

 sudo find / -name mosquitto.conf 

enter image description here

0
source

Since I do not have enough points to comment

 #!/bin/sh echo "cleaning " $1 " :: usage: cleanmqtt <host>" mosquitto_sub -h $1 -t "#" -v | while read line; do mosquitto_pub -h $1 -t "${line% *}" -r -n; done 

may cause an infinite loop due to pub / sub delays. Adding --retained-only to mosquitto_sub seems to remove the endless loop.

0
source

All Articles