How to delete all devices from Azure IoT Hub using Azure CLI?
Asked Answered
S

4

5

I have an Azure IoT Hub with a bunch of devices that our team's E2E tests generate. I want to purge the Hub every once in a while using the Azure CLI.

I am running the Azure CLI locally on Powershell, using the Azure IoT extension.

From my research, there is a way to get a list of all the devices in a hub printed to the console in JSON format:

az iot hub device-identity list --hub-name "test-hub"

And there is a way to delete a single device-identity:

az iot hub device-identity delete --device-id "test-id" --hub-name "test-hub"

How can I delete all the devices in the hub using the cli interfaces and some powershell commands?

Sean answered 12/6, 2019 at 17:11 Comment(1)
Can you show a sample output for az iot hub device-identity list --hub-name "test-hub" ?Rumsey
R
4

Just run a For loop in PowerShell.

First install the Azure CLI for Powershell:

Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'

Then add the Azure IoT extensions modules for PowerShell, log in to Azure, and change to the appropriate subscription (change <subscription_id>) :

az extension add --name azure-cli-iot-ext
az login
az account set -s <subscription_id>

After that, run the following Foreach loop which will delete all devices (change test-hub) :

$iotHubName = "test-hub"
$json = az iot hub device-identity list --hub-name $iotHubName
$devices = $json | ConvertFrom-Json
Foreach ($device in $devices)
{
  az iot hub device-identity delete --device-id $device.deviceId --hub-name $iotHubName
}

Note: This is an extremely slow process as of 2019. You can follow the progress by looking up the IoT devices in the main portal.azure UI.

Riot answered 27/9, 2019 at 6:0 Comment(0)
B
1

This does not seem possible today with just one command. The underlying REST interface (which is what the cli and everything else uses) also does not have a bulk delete: https://learn.microsoft.com/en-us/rest/api/iothub/service/deletedevice

The IoT extension Github has some automation samples: https://github.com/Azure/azure-iot-cli-extension/blob/dev/docs/scenario-automation.md

There they mass-create devices using a simple for loop. You can probably reuse some of that and combine it with the az iot hub device-identity list command

Bract answered 12/6, 2019 at 17:19 Comment(0)
L
1

In addition to @silent, the Azure IoT Hub supports an Export/Import Devices bulk job described in the blob. Have a look at the following links:

Iot Hub Resource - Import Devices

Import devices example – bulk deletion

Basically, calling the Export Devices will create a blob of all devices, then updating this list for each device with an ImportMode.Delete mode, the blob is ready for invoking an Import Devices bulk job. In the case when all devices are well-known deviceIDs, the Export Devices step can be skipped and using a predefined input blob.

Note, that the bulk job is a long running background process, so we can used polling its status or using an Azure Event Grid for IoT Hub eventing. Deleting 100 devices will take approximately 1 minute.

Litta answered 14/6, 2019 at 7:4 Comment(0)
S
0

Another method, building on @goamn's answer, but using ForEach-Object -Parallel:

$iotHubName = "test-hub"
$json = az iot hub device-identity list --hub-name $iotHubName
$devices = $json | ConvertFrom-Json
$device | ForEach-Object -Parallel { az iot hub device-identity delete --device-id $_.deviceId --hub-name "test-hub" } -ThrottleLimit 10

Some notes:

  • Requires Powershell 7+
  • The az iot hub device-identity list call only seems to return 1000 devices. Maybe there's a way to return all of them, I haven't investigated as I only have 2000 to delete.
  • The parallel script block doesn't seem to be in the same scope as the $iotHubName variable for some reason (also haven't investigated), so you need to add the hub name there as well.
  • Running this in parallel can lead to lock file issues and other errors. But hey, if it works and deletes 90% of the devices 10 (or 50?) times faster then you're still ahead, right? Rinse & repeat until they're all gone.
Superhighway answered 9/11, 2022 at 4:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.