I've seen the mosquitto_pub -h [server] -r -n -t [XYZ]
syntax for clearing out one off messages. My problem is the device developers have posted a lot of garbage messages.
I have a Java/Paho code base that I'd like to modify to do this automatically as needed, but I can't seem to publish a zero byte message. I tried
client.publish(topic,null);
...but that didn't seem to work.
Any suggestions on how to delete everything, en mass?
Here is how to do it properly with a shell script.
#!/bin/sh
echo "cleaning " $1 " :: usage: cleanmqtt <host>"
mosquitto_sub -h $1 -t "#" -v --retained-only | while read line; do mosquitto_pub -h $1 -t "${line% *}" -r -n; done
Just put it in a file called somthing 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
This solution is quite crude. You cant specify what to delete or anything. You may have to abort with ctrl-c after you can assume that it has received all the messages.
There are 2 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);
The other option would be to stop mosquitto and delete the persistence file and restart
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With