2
votes

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?

3
Can you show a sample output for az iot hub device-identity list --hub-name "test-hub" ?Saurabh P Bhandari

3 Answers

2
votes

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.

1
votes

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://docs.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

0
votes

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.