0

There is an solution in the Stack overflow that contains the commands for deleting the resources in a resource group but it's not working for us.

In the Azure Cloud Shell, When using the command given from above link, getting the error as:

unrecognized arguments: ConvertFron-Json Foreach-Object {az resource delete --ids /usr/bin/cloudshellhelp.id --verbose

for the command: az resource list --resource-group senthil-b-rg ConvertFrom-Json Foreach-Object {az resource delete --resource-group senthil-b-rg --ids $_.id --verbose}

If I keep pipe (|) symbol in the command:

az resource list --resource-group senthil-b-rg \
| ConvertFrom-Json | Foreach-Object {az resource delete --resource-group senthil-b-rg --ids $_.id --verbose}

Errors are

bash:ConvertFrom-Json: command not found
bash: Foreach-Object: command not found
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipleError: [Errno 32] Broken pipe
  • 3
    You’re mixing PowerShell and CLI commands. – Gaurav Mantri Aug 13 '22 at 15:18
  • Oh, I'm new to CLI & PowerShell! Could you give me some hint on how to resolve this @GauravMantri –  Aug 13 '22 at 15:30
  • 1
    Using azoure cloud shell, you could choose bash or powershell, if you select powershell it should work. – Thomas Aug 14 '22 at 04:39
  • `Content: {"Code":"Conflict","Message":"Server farm 'webappserviceplan' cannot be deleted because it has web app(s) azjavawebapp assigned to it.` `Some resources failed to be deleted` - Got this message while running the command in Azure Cloud Shell (PowerShell) but all the resources inside the resource group were deleted! –  Aug 14 '22 at 14:05
  • To avoid this conflict message, do we need to delete the resources using order like 1st web app, next is app service plan as given in the same [solution](https://stackoverflow.com/a/63745410/19615125)? –  Aug 14 '22 at 14:07

1 Answers1

0

I tried to reproduce the same in my environment and got the same error as below:

az resource list --resource-group myresourcegroup \
| ConvertFrom-Json | Foreach-Object {az resource delete --resource-group myresourcegroup --ids $_.id --verbose}

Response:

enter image description here

In my resource group, I have resources like below:

enter image description here

To resolve the error, I tried to run the same command in PowerShell and got the same warning as below:

enter image description here

But the resources got deleted in the Portal:

enter image description here

Please note that, while deleting the resources you need to follow the order:

  • Make sure to delete the child (nested) resources initially.
  • Delete the resources that manage the other resources.

Reference:

Delete resource group and resources - Azure Resource Manager | Microsoft Docs

UPDATE:

To get rid of the warning, you need to add --only-show-errors in the command like below:

$resources = az resource list --resource-group myresourcegroup | ConvertFrom-Json  
foreach ($resource in $resources) {  
az resource delete --resource-group myresourcegroup --ids $resource.id --only-show-errors  
}

Response:

enter image description here

Make sure to remove --verbose as --only-show-errors doesn't work with it.

Rukmini
  • 6,015
  • 2
  • 4
  • 14