Mass Delete Devices

So we run multiple networks at work, and I inadvertently enabled discovery polling of devices. We use a separate system to monitor servers, but Libre to monitor core network devices.

Is there any way to mass delete these devices that have been autodiscovered? There’s hundreds of them! :smiley:

Dan

1 Like

./delhost.php $hostname

Do that in a for loop with a list of all devices to remove.

Perfect, thanks! (Sorry for the delay)

How do you create this loop? I have about 400+ devices I need to delete.

Their is lots of examples online, just search for bash for loop.

Thanks, I’ll dig into it. Is there any way in the GUI to delete multiple devices at once?

Nope its really easy to do it on command line though.

if you don’t want to make a bash loop, then load up a spreadsheet with all your devices IPs in, just affix ./delhost to a new column before the IPs and copy paste the whole thing into prompt.

Note if your using the autodiscovery in librenms, then they will be re-added automagically so you may want to turn that feature off.

1 Like

how do i delete a whole subnet?

made this a long time ago, code isnt good looking but it does the job :slight_smile:

usage ./massdeletedevice.php

just change “ip.ip.ip.” to whatever range you want to delete, for loop goes upto 254 last octet

cat massdeletedevice.php

#!/usr/bin/env php

<?php $init_modules = array(); require __DIR__ . '/includes/init.php'; $ipv4 = "ip.ip.ip."; $doesntexist = "doesn't exist!"; for ($hosts = 1; $hosts < 254; $hosts = $hosts + 1) { /*echo $ipv4.$hosts;*/ $host = $ipv4.$hosts; $id = getidbyname($host); if ($id) { echo delete_device($id)."\n"; } else { echo $host." - ".$doesntexist."\n"; } } ?>
1 Like

Do next steeps:

  1. Putting in a text file the names of the host to be deleted: Execute next command in shell:

mysql -u root -p librenms_database_name -e "select hostname from devices where hostname like ‘your_hostname-matching_criteria’ into outfile ‘name_of_the_text_file’ ;"

(you will be asked for user password)
(your_hostname_matchng_criteria: follow the select like syntax)

  1. Proceed with deletion: as root (use sudo if needed) execute in bash:

for i in cat name_of_the_text_file; do sudo /opt/librenms/delhost.php $i; done

Thats’s all. Take into account:

  • librenms: this is the name of librenms database. Change as appropriate.
  • name_of_the_text_file: you can use any file name and it will be stored into the database directory. Be carefull to don’t overwrite database or other existing files or your db will become broken…

To do it easier, put the next lines in a bash script and execute it:

---- Start bash script code ----
#!/bin/bash

echo “±- LIBRENMS ------------------------------------”
echo “| Bulk device deletion:”
echo “|”
echo “| Devices to be deleted: $1”
echo “|”
echo “±-----------------------------------------------”
echo “”

if [ $(whoami) != “root” ]; then
echo “This must be executed as root!”
exit 100
fi

if [ ${#2} -eq 0 ]; then
tmpfile=$(mktemp /tmp/librenms-del-host.XXXXXX)
else
tmpfile="$2"
fi

if [ -f $tmpfile ]; then
rm $tmpfile
fi

SqlStm=""
SqlStm=“select hostname from devices where hostname like '”$1"’ into outfile ‘"$tmpfile"’ ;"

echo “Enter password for root@localhost”
mysql -u root -p librenms -e “$SqlStm”

if [ -f $tmpfile ]; then
for i in cat $tmpfile; do sudo /opt/librenms/delhost.php $i; done
fi
---- End bash script code —

hope help you