diff --git a/src/AzureIoTHub.Portal/Server/Controllers/DeviceModelsController.cs b/src/AzureIoTHub.Portal/Server/Controllers/DeviceModelsController.cs index 3f9137d64..abd277666 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/DeviceModelsController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/DeviceModelsController.cs @@ -133,7 +133,9 @@ public async Task Put([FromForm] string deviceModel, [FromForm] I [HttpDelete("{deviceModelID}")] public async Task Delete(string deviceModelID) { + // we get all devices var deviceList = await this.devicesService.GetAllDevice(); + // we get the device model with a query var query = this.tableClientFactory .GetDeviceTemplates() .Query(t => t.RowKey == deviceModelID); @@ -145,6 +147,10 @@ public async Task Delete(string deviceModelID) var deviceModel = this.deviceModelMapper.CreateDeviceModel(query.Single()); + var queryCommand = this.tableClientFactory + .GetDeviceCommands() + .Query(t => t.PartitionKey == deviceModel.ModelId); + foreach (var twin in deviceList) { if (DeviceHelper.RetrieveTagValue(twin, "modelId") == deviceModel.ModelId) @@ -153,6 +159,18 @@ public async Task Delete(string deviceModelID) } } + var commands = queryCommand.Select(item => this.deviceModelCommandMapper.GetDeviceModelCommand(item)).ToList(); + + // if we have command + if (commands.Count > 0) + { + foreach (var item in commands) + { + _ = await this.tableClientFactory + .GetDeviceCommands().DeleteEntityAsync(deviceModelID, item.CommandId); + } + } + var result = await this.tableClientFactory .GetDeviceTemplates() .DeleteEntityAsync(DefaultPartitionKey, deviceModelID);