12
votes

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?

7
This might help: mosquitto_sub -t '#' --remove-retained --retained-only. See also: mosquitto.org/man/mosquitto_sub-1.htmlrel

7 Answers

6
votes

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

17
votes

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.

1
votes

This should work:

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

Also, you may be interested in this script from Eclipse Paho Python, to clear a given topic hierarchy. You may want to implement a similar behavior in Java, but it looks like you may be looking for a one-off solution, so maybe just use the Python script :)

1
votes

Since I don't have enough points to comment, running

#!/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

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

1
votes

for powershell users

had this issue on windows, so here with powershell. Mosquitto needs to be installed on the command host.

get retained messages

I did not want to clear all retained messages. Example, only the ones containing "octo" in it's topic. Let's see what's there:

mosquitto_sub.exe -h <mqtt host> -v -u <mqtt user> -P <mqtt password> -t '#' --retained-only|
Select-String octo

(replace mqtt host, user, password as needed)

delete retained messages

use the same search string here ( in this example "octo"):

mosquitto_sub.exe -h <mqtt host> -v -u <mqtt user> -P <mqtt password> -t '#' --retained-only|
Select-String octo |
Out-String -Stream|ForEach-Object -Process {$_.Split(" ")[0]}|
%{mosquitto_pub.exe -h <mqtt host> -u <mqtt user> -P <mqtt password> -t "$_" -r -n}

Again, replace all occurences of mqtt host, user, password.

Simply check again with step 1 if anything is left :)

0
votes

If you are using the Mosquitto MQTT Broker, disable "Retained messages" using the official method, provided by Mosquitto

First find mosquitto.conf file

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

Edit with your favorite text editor, mine is nano.

sudo nano /etc/mosquitto/mosquitto.conf

and in that file replace "persistence false" in place of "persistence true"

persistence false

Now save file (if using nano press ctrl+o and then enter to save, ctrl+x to exit)

now restart mosquitto using below commands

sudo service mosquitto stop
sudo service mosquitto start

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

sudo find / -name mosquitto.conf

enter image description here

0
votes

I also don't have enough rep to add a simple comment. The bash script is great! The edge case I ran into is one where multiple spaces exist, such as a date stamp:

bob/was/here/time 2021-08-31 01:52:51

${line% *} only removes the time while including the date with the topic. Adding a second % to ${line%% *} makes the substitution greedy, ensuring that only the topic is returned.