DEV Community

Dylan Morley
Dylan Morley

Posted on

AZ CLI - Deleting Terraform Test Resource Groups

This is a series of quick posts, tips and tricks when working with the Azure CLI.

Deleting Multiple Resource Groups

When working with the Terraform Provider for Azure, you may be adding a new feature that requires you to run the test automation packs.

In cases where there are errors in the Provider as you're developing the feature, you might end up with errors like so

=== CONT  TestAccEventGridEventSubscription_deliveryMappings
testcase.go:88: Step 1/2 error: Error running pre-apply refresh: exit status 1

        Error: Argument or block definition required

          on terraform_plugin_test.tf line 47, in resource "azurerm_eventgrid_event_subscription" "test":
          47:     [

        An argument or block definition is required here.

    testing_new.go:63: Error retrieving state, there may be dangling resources: exit status 1

--- FAIL: TestAccEventGridEventSubscription_deliveryMappings (10.18s)
FAIL      
Enter fullscreen mode Exit fullscreen mode

Note part of the error description - there may be dangling resources:

Because the test didn't complete, the destroy phase of the test might not have completed, which means you're in a state where there are resources in Azure that have been created by the test code that need tidying up

Luckily, we can do this with a bit of Powershell + AZ CLI.

$resource_groups=(az group list --query "[? contains(name,'acctest')][].{name:name}" -o tsv)

foreach ($group in $resource_groups) {
    Write-Output "Deleting resource group $group"

    az group delete -n $group --yes 
}
Enter fullscreen mode Exit fullscreen mode
  • Assumes you're logged in and in the subscription you want to work with

Terraform tests create resource groups following an acctest naming convention, so we find all of those that match, then delete them one by one.

Discussion (0)