Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion for docs: Deleting retained messages in bulk #116

Open
TotallyInformation opened this issue Jun 22, 2017 · 1 comment
Open

Suggestion for docs: Deleting retained messages in bulk #116

TotallyInformation opened this issue Jun 22, 2017 · 1 comment

Comments

@TotallyInformation
Copy link

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
Copy link
Owner

kamilfb commented Jun 30, 2017

Cool - thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants