Skip to content

Instantly share code, notes, and snippets.

@k-popov
Created January 10, 2018 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k-popov/7962cb0bb7eb633d59c1c0a914aa752f to your computer and use it in GitHub Desktop.
Save k-popov/7962cb0bb7eb633d59c1c0a914aa752f to your computer and use it in GitHub Desktop.

Usage:

az resource list | python sort_for_deletion_parallel.py '^resources-to-delete.*' | while read CMD; do bash -xc "$CMD"; done
import json
import sys
import re
del_seq = [
# 'Microsoft.Compute/virtualMachines/extensions',
'Microsoft.Compute/virtualMachines',
'Microsoft.Compute/virtualMachineScaleSets',
'Microsoft.Compute/availabilitySets',
'Microsoft.Network/loadBalancers',
'Microsoft.Network/networkInterfaces',
'Microsoft.Network/publicIPAddresses',
'Microsoft.Network/networkSecurityGroups',
'Microsoft.Network/virtualNetworks',
'Microsoft.Compute/disks',
]
if len(sys.argv) < 2:
sys.stderr.write("First positional argument must be filter regex")
sys.exit(1)
filter_regex = re.compile(sys.argv[1])
all_res = json.load(sys.stdin)
parallel_del_list = []
for res_type in del_seq:
parallel_del_list.append([_ for _ in all_res if _['type'] == res_type])
for res_of_type in parallel_del_list:
parallel_del_cmd_list = []
for res in res_of_type:
if not filter_regex.match(res['name']):
continue
del_cmd = "az resource delete --resource-group {0} --name {1} --resource-type {2}".format(
res['resourceGroup'],
res['name'],
res['type']
)
parallel_del_cmd_list.append(del_cmd)
print " & ".join(parallel_del_cmd_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment