Just for reference,
The following perl script will purge all messages from a topic and sub topics below it. You need to install mosquitto_sub and mosquitto_pub first, then run the script as mqtt_purge.pl [mqtt_ip] [user] [password] [topic_top]
It works by pulling retained messages one by one, waiting up to 10 seconds for each one. If it times out, it stops, otherwise, it parses the message, grabs the topic from it, and sends a NULL message. Then the next message is pulled and so on.
mqtt_purge.pl
#!/bin/env perl
my $mqtt_server = $ARGV[0];
my $USER = $ARGV[1];
my $PASS = $ARGV[2];
my $topic_top = $ARGV[3];
print STDERR "\nRemoving old messages from $topic_top/#\n";
my $outfile = "/tmp/next.txt";
while (1) {
my $com = "mosquitto_sub -t '$topic_top/#' -h $mqtt_server -u '$USER' -P '$PASS' " .
"-v -C 1 -W 10 --retained-only >$outfile";
print STDERR "RUN $com\n";
my $err = system($com);
last if $err || ! -e $outfile;
my $buff = '';
open my $fh, "<", $outfile;
last if ! defined $fh;
read $fh, $buff, 128, length($buff);
close $fh;
my $topic = (split(/ /, $buff))[0];
last if !$topic;
print STDERR "clear topic $topic\n";
$com = "mosquitto_pub -t '$topic' -n -r -h $mqtt_server -u '$USER' -P '$PASS'";
$err = system($com);
last if $err;
}
unlink $outfile;
print STDERR "\nALL CLEARED !!\n\n";