javamqttmosquittopaho

How to clear ALL retained mqtt messages from Mosquitto?


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?


Solution

  • 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