Easiest way to remove MQTT device?

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";

Now you can do this…

imagen

10 Likes

Because of an Issue with OpenMqttGateway i had the same problem, that i had to remove many mqtt devices in HA. My Solution is to execute the following Bash Script #!/bin/bash

MQTT Broker Informationen

BROKER_HOST=“IP”
BROKER_PORT=“1883”
USERNAME=“user”
PASSWORD=“password”

Überprüfe, ob der Suchbegriff als Argument übergeben wurde

if [[ -z “$1” ]]; then
echo “Bitte geben Sie einen Suchbegriff als Argument ein.”
exit 1
fi

Suchbegriff aus dem ersten Argument extrahieren

SEARCH_TERM="$1"

Zeitbegrenzung in Sekunden (z.B. 60 für eine Minute)

TIMEOUT=10

Temporäre Datei für Ausgabe

TMP_FILE=$(mktemp)

MQTT Subscription

mosquitto_sub -h “$BROKER_HOST” -p “$BROKER_PORT” -u “$USERNAME” -P “$PASSWORD” -t “homeassistant/#” -v > “$TMP_FILE” &

Debugging-Ausgabe

echo “Waiting for subscription to complete…”

Warte für die angegebene Zeit

sleep $TIMEOUT

Debugging-Ausgabe

echo “Subscription completed.”

Beenden des MQTT-Abonnements

kill $!

Schleife durch die Liste der Topics und sende Löschbefehle

#cat “$TMP_FILE” | awk ‘{print $1}’ | grep “$SEARCH_TERM” | sed ‘s//config$//’ | while IFS= read -r topic; do
cat “$TMP_FILE” | awk ‘{print $1}’ | grep “$SEARCH_TERM” | while IFS= read -r topic; do
mosquitto_pub -h “$BROKER_HOST” -p “$BROKER_PORT” -u “$USERNAME” -P “$PASSWORD” -t “$topic” -r -m “”
echo “Löschbefehl für Topic: $topic gesendet”
done

Temporäre Datei löschen

rm “$TMP_FILE”
with the parameter of the part of the topic name that is to be deleted. If you save this script as delete_mqtt-devices.sh then run it for example in this way: ./delete_mqtt-devices.sh skylink. In this example all devices which have a name that is including skylink will be deleted.

Hi, thank you for the right way to do it!

This is what drives me crazy with HA sometimes, why would the “DELETE” button be greyed out in the device info?
Oh no, let’s make it incredibly unintuitive for this easy task.
Why doesent the delete button give the info to do this (or work the same way)?
Instead its greedy out, and i go through all 45 comments and procedures before i found your post…
Sorry, needed to vent, but still think it’s bad UI practice that the delete button is greyed out.

Only entities created by users are deletable by users. Entities created by integrations are managed by the integration. Not sure why, maybe it would break the integration if the entity suddenly disappeared. I guess that’s why you can disable them instead.