kamilfb / mqtt-spy

Please use the new Eclipse Paho GitHub repo instead.
https://github.com/eclipse/paho.mqtt-spy/wiki
346 stars 68 forks source link

Suggestion for docs: Deleting retained messages in bulk #116

Open TotallyInformation opened 7 years ago

TotallyInformation commented 7 years ago

Hi, it took me a while to work this out.

I needed to delete a bunch of retained messages and struggled to get the right parameters. Here is the answer: mqttspy.publish(topic, '', 0, true);

The trick is that you have to pass the retain parameter as true which is rather counter-intuitive.

Here is a script to delete any number of topics. You can export a topic list from the UI of course:

// Delete a bunch of retained messages

// Wrap the script in a method, so that you can do "return false;" in case of an error or stop request
function publish() {
  var Thread = Java.type("java.lang.Thread");

  // Define an array for topics we want to process
  var topics = [
    'topic1',
    'topic2'
  ];

  // Loop through each topic
  topics.forEach(function(topic) {
    // Publish blank payload with retain false
    // removes retained messages
    mqttspy.publish(topic, '', 0, true);

    // Add some sleepy time and allow
    // mqtt_spy to interrupt if needed
    try {
      Thread.sleep(1000);
    } catch(err) {
      return false;
    }
  });

  // This means all OK, script has completed without any issues and as expected
  // return false if needed
  return true;
}

publish();

//EOF

Many thanks again for MQTT-Spy, a great tool! :)

kamilfb commented 7 years ago

Cool - thank you!