diff --git a/sdk/digitaltwins/Azure.DigitalTwins.Core/samples/DigitalTwinsClientSample/DigitalTwinsLifecycleSamples.cs b/sdk/digitaltwins/Azure.DigitalTwins.Core/samples/DigitalTwinsClientSample/DigitalTwinsLifecycleSamples.cs index 74a220bb792fe..15bd0c199e62c 100644 --- a/sdk/digitaltwins/Azure.DigitalTwins.Core/samples/DigitalTwinsClientSample/DigitalTwinsLifecycleSamples.cs +++ b/sdk/digitaltwins/Azure.DigitalTwins.Core/samples/DigitalTwinsClientSample/DigitalTwinsLifecycleSamples.cs @@ -196,19 +196,21 @@ public async Task DeleteTwinsAsync() foreach (KeyValuePair twin in twins) { + var digitalTwinId = twin.Key; + try { // Delete all relationships - AsyncPageable relationships = client.GetRelationshipsAsync(twin.Key); + AsyncPageable relationships = client.GetRelationshipsAsync(digitalTwinId); await foreach (var relationshipJson in relationships) { BasicRelationship relationship = JsonSerializer.Deserialize(relationshipJson); - await client.DeleteRelationshipAsync(twin.Key, relationship.Id); + await client.DeleteRelationshipAsync(digitalTwinId, relationship.Id); Console.WriteLine($"Found and deleted relationship with Id {relationship.Id}."); } // Delete any incoming relationships - AsyncPageable incomingRelationships = client.GetIncomingRelationshipsAsync(twin.Key); + AsyncPageable incomingRelationships = client.GetIncomingRelationshipsAsync(digitalTwinId); await foreach (IncomingRelationship incomingRelationship in incomingRelationships) { @@ -217,7 +219,6 @@ public async Task DeleteTwinsAsync() } // Now the digital twin should be safe to delete - string digitalTwinId = twin.Key; #region Snippet:DigitalTwinsSampleDeleteTwin @@ -233,7 +234,7 @@ public async Task DeleteTwinsAsync() } catch (RequestFailedException ex) { - FatalError($"Failed to delete {twin.Key} due to {ex.Message}"); + FatalError($"Failed to delete {digitalTwinId} due to {ex.Message}"); } } }