2

I want to delete all the the resource groups except the one used by cloud shell.

$ az group list --query [].name --output tsv | grep -i -v cloud >> resGroups.txt

$ while read RGS; do echo $RGS;done<resGroups.txt
DefaultResourceGroup-EUS
NetworkWatcherRG

$ while read RGS; do az group delete -n $RGS --no-wait -y;done<resGroups.txt
validation error: Parameter 'resource_group_name' must conform to the following pattern: '^[-\\w\\._\\(\\)]+$'.
validation error: Parameter 'resource_group_name' must conform to the following pattern: '^[-\\w\\._\\(\\)]+$'.

$ while read RGS; do echo $RGS | sha256sum ;done<resGroups.txt
8ea64f061e7d52cd16b4c8a89e55ce06275c538ed91a0544b9db720281350dc2  -
b3bc010b955260f143c4b2d860020b36de391bddcbbdf31ce5a608d705f25829  -

$ echo DefaultResourceGroup-EUS | sha256sum
c06416f056e76d1c6642a3df6dd646a91b46ee40791b04db32d9c374e7ab790d  -

$ echo NetworkWatcherRG | sha256sum
c0aee8c9e5d3be7fe8a0748031ca1f5f762b2f6aa6a5e3de95dc0b1dbbc54120  -

Based on the error and just doing an echo, it's looking like the loop is working but there's something wrong with how the names are getting printed to the file.

I don't understand why the hashes don't match.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxx SOLUTION xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Thanks to everyone for their input, it got me thinking and I realized the issue is how I'm outputting the string.

This doesn't work because it adds a tab at the end of each string:

notRG=$(az group list --query "[?contains(name, 'cloud')].name" --output tsv)

This will delete every resource group in your Azure account except any that include the word cloud:

notRG=$(az group list --query "[?contains(name, 'cloud')].name" --output json | jq .[] -r)
for i in $(az group list --query "[?name!='$notRG'].name" --output json | jq .[] -r); do az group delete -n $i -y; done

When I kicked off the loop:

$ az group list | grep -i "name\|state"
    "name": "myResourceGroup",
      "provisioningState": "Deleting"
    "name": "DefaultResourceGroup-EUS",
      "provisioningState": "Succeeded"
    "name": "MC_myResourceGroup_myAKSCluster_eastus",
      "provisioningState": "Deleting"
    "name": "NetworkWatcherRG",
      "provisioningState": "Succeeded"
    "name": "cloud-shell-storage-westus",
      "provisioningState": "Succeeded"

After the loop completed:

$ az group list | grep -i "name\|state"
    "name": "cloud-shell-storage-westus",
      "provisioningState": "Succeeded"
Aaron
  • 21
  • 5
  • Not sure if there is a reason you are using outputting the first part to a file. Have you looked into JQ. It's great for processing json, and you can do loops straight from it. So you could easily save the JSON to a file and have it processed by JQ and use it for a loop. https://starkandwayne.com/blog/bash-for-loop-over-json-array-using-jq/ – Ron Jul 14 '20 at 03:59

2 Answers2

0

Delete resource group with name:

az group delete --name ExampleResourceGroup

I have tried !("groupName") and ^((?!groupName).)*$ to replace ExampleResourceGroup, but the error(validation error) is still reported.

So I use the if-statement to except. You could add the code in the loop.

groupName='xxxxxxxx';
if [ $groupName!='the group name that you want to except' ]; then az group delete -n=$groupName; fi;

I try this with az group show, and it works well.

enter image description here

unknown
  • 6,778
  • 1
  • 5
  • 14
  • This didn't work for me either :( It turns out the issue is with how the Azure CLI outputs the string. – Aaron Jul 15 '20 at 16:14
0

xxxxxxxxxxxxxxxxxxxxxxxxxxxxx SOLUTION xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Thanks to everyone for their input, it got me thinking and I realized the issue is how I'm outputting the string.

This doesn't work because it adds a tab at the end of each string:

notRG=$(az group list --query "[?contains(name, 'cloud')].name" --output tsv)

This will delete every resource group in your Azure account except any that include the word cloud:

notRG=$(az group list --query "[?contains(name, 'cloud')].name" --output json | jq .[] -r)
for i in $(az group list --query "[?name!='$notRG'].name" --output json | jq .[] -r); do az group delete -n $i -y; done

When I kicked off the loop:

$ az group list | grep -i "name\|state"
    "name": "myResourceGroup",
      "provisioningState": "Deleting"
    "name": "DefaultResourceGroup-EUS",
      "provisioningState": "Succeeded"
    "name": "MC_myResourceGroup_myAKSCluster_eastus",
      "provisioningState": "Deleting"
    "name": "NetworkWatcherRG",
      "provisioningState": "Succeeded"
    "name": "cloud-shell-storage-westus",
      "provisioningState": "Succeeded"

After the loop completed:

$ az group list | grep -i "name\|state"
    "name": "cloud-shell-storage-westus",
      "provisioningState": "Succeeded"
Aaron
  • 21
  • 5