protected void CleanupResourceGroups() { if (Mode != RecordedTestMode.Playback) { Parallel.ForEach(ResourceGroupCleanupPolicy.ResourceGroupsCreated, resourceGroup => { try { var sub = _cleanupClient.GetSubscriptions().GetIfExists(TestEnvironment.SubscriptionId); sub.Value?.GetResourceGroups().Get(resourceGroup).Value.StartDelete(); } catch (RequestFailedException e) when(e.Status == 404) { //we assume the test case cleaned it up if it no longer exists. } }); Parallel.ForEach(ManagementGroupCleanupPolicy.ManagementGroupsCreated, mgmtGroupId => { try { _cleanupClient.GetManagementGroupOperations(mgmtGroupId).StartDelete(); } catch (RequestFailedException e) when(e.Status == 404 || e.Status == 403) { //we assume the test case cleaned it up if it no longer exists. } }); } }