How to Bulk Delete ThingsBoard Devices by Prefix Using Python

In this post, I will show how to safely bulk delete ThingsBoard devices by name prefix using Python.

This is useful in test environments where a large number of virtual devices are created automatically.


Background

In my previous post:

I demonstrated how to treat stock symbols as virtual IoT devices in ThingsBoard.

This creates device names such as:

stock.AAPL
stock.MSFT
stock.TSLA

When running with:

stocks:
  - symbol: "*"

you may quickly create 10,000+ devices.


The Problem

ThingsBoard UI is not suitable for large-scale deletion:

  • Only supports selecting devices on the current page
  • No practical way to delete thousands of matched devices
  • Gateway devices are also devices

If you delete blindly, you may also delete your gateway device.


Recommended Approach

Use the ThingsBoard REST API via Python.

The script below:

  • collects matching device IDs first
  • deletes only devices with the configured prefix
  • skips gateway devices
  • adds a small delay between delete requests

Python Environment Setup on Ubuntu

On modern Ubuntu systems, installing Python packages globally may fail because of Python’s externally managed environment restriction.

Use a virtual environment instead:

sudo apt update
sudo apt install -y python3-venv python3-pip

python3 -m venv tb-cleanup-venv
source tb-cleanup-venv/bin/activate

pip install --upgrade pip
pip install tb-rest-client

Bulk Delete Script

Create a file:

vi delete_thingsboard_devices_by_prefix.py

Paste the following script:

from tb_rest_client.rest_client_ce import RestClientCE
import time

TB_URL = "https://tb.maksonlee.com"
USERNAME = "tenant@thingsboard.org"
PASSWORD = "your_password"

PAGE_SIZE = 100
DELETE_DELAY = 0.01
DEVICE_NAME_PREFIX = "stock."

with RestClientCE(base_url=TB_URL) as rest_client:
    rest_client.login(username=USERNAME, password=PASSWORD)

    devices_to_delete = []
    page = 0

    while True:
        page_data = rest_client.get_tenant_devices(
            page_size=PAGE_SIZE,
            page=page
        )

        devices = page_data.data

        if not devices:
            break

        for device in devices:
            is_gateway = False

            if getattr(device, "additional_info", None):
                is_gateway = device.additional_info.get("gateway", False)

            if is_gateway:
                print(f"Skipping gateway device: {device.name}")
                continue

            if not device.name.startswith(DEVICE_NAME_PREFIX):
                continue

            devices_to_delete.append((device.id.id, device.name))

        if not page_data.has_next:
            break

        page += 1

    print(f"Found {len(devices_to_delete)} devices to delete.")

    for device_id, device_name in devices_to_delete:
        print(f"Deleting {device_name} / {device_id}")
        rest_client.delete_device(device_id)
        time.sleep(DELETE_DELAY)

    print(f"Done. Deleted {len(devices_to_delete)} devices.")

Update these values:

TB_URL = "https://tb.maksonlee.com"
USERNAME = "tenant@thingsboard.org"
PASSWORD = "your_password"
DEVICE_NAME_PREFIX = "stock."

Run the Script

Make sure the virtual environment is activated:

source tb-cleanup-venv/bin/activate

Run:

python delete_thingsboard_devices_by_prefix.py

Example output:

Skipping gateway device: stock-collector-01
Found 10342 devices to delete.
Deleting stock.AAPL / 4f3e...
Deleting stock.MSFT / 51c2...
Done. Deleted 10342 devices.

Adjust the Delete Speed

The script uses this delay between delete requests:

DELETE_DELAY = 0.01

If your ThingsBoard server is under load, increase it:

DELETE_DELAY = 0.05

or:

DELETE_DELAY = 0.1

Did this guide save you time?

Support this site

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top