home-assistant

Erase Old MQTT or Unwanted Sensors

A quick way to bulk-delete unwanted devices in Home Assistant using a staging Area plus a short browser-console script.

Home Assistant makes it easy to add devices and surprisingly tedious to remove a lot of them at once โ€” especially stale MQTT sensors. Here's the approach I use to delete them in bulk.

The idea

Rather than deleting devices one by one, you herd everything you want gone into a single Area, then run a short script in your browser's console that deletes everything in that Area.

Steps

  1. Create a staging Area. In Home Assistant, make a new Area โ€” I call mine Garbage โ€” and note its ID. You'll set that as the GARBAGE_ID variable in the script.
  2. Move the junk in. In the devices list, check off the devices you want to remove, then use the menu in the upper right to mass-assign them to the staging Area.
  3. Run the script. Open your browser's developer console and paste in the script. It calls Home Assistant's WebSocket API to read the device registry, filter to your staging Area, and remove those devices from their config entries, logging results as it goes.

Tip: The script ships with an actually_delete flag set to false. Leave it off for a dry run first โ€” you'll see exactly which devices would be deleted before anything is removed. Flip it to true once you're happy.

The script

The full, current script lives in this gist:

github.com/jasonk โ€” erase-old-mqtt gist โ†—

Copy it into your console after you've staged the devices, do a dry run, then delete for real.