This repository was archived by the owner on Mar 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_rg_by_pattern.sh
More file actions
executable file
·79 lines (66 loc) · 2.42 KB
/
delete_rg_by_pattern.sh
File metadata and controls
executable file
·79 lines (66 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source ${DIR}/functions.sh
print-usage() {
echo "
This script deletes resource groups whose name matches given pattern on the given subscription if there is no meaningful activites on the resource group in the given length of time..
The script takes 4 arguments: Id of the subscription to work on, pattern to match resource group's name for deletion, length of time to check, and/or whether it's a 'realrun'.
In dryrun mode, resource groups will be examed but not deleted. Any string other than 'realrun' will result in dryrun.
e.g.
$0 'my-test-subscrioption' 'test-resource-group' 4h --> dry run
$0 'my-test-subscrioption' 'pattern' 4h realrun --> real run
$0 'my-test-subscrioption' 'pattern' 4h realrun | tee ./log --> real run with logs recorded
"
}
# Errors
INVALID_ARGS=8001
if (( $# < 2 )); then
print-usage
exit ${INVALID_ARGS}
fi
subId=$1
pattern=$2
length=$3
realRun=0
if (( $# == 4 )) && [ $4 == 'realrun' ]; then
realRun=1
fi
echo "Delete resource groups with no activity in last '${length}' matching '${pattern}' in subscription '${subId}' 'realrun'=${realRun}"
cmd="az group list --subscription '${subId}' --query \"[?contains(name, '$pattern')].{name:name, location:location, managedBy:managedBy, state:properties.provisioningState, preserve:tags.preserve}\" -o tsv"
echo "$cmd"
rgs=`eval ${cmd}`
rgCnt=$(echo "$rgs" | wc -l)
echo "FOUND "${rgCnt}" RGs MATCHING '${pattern}' IN SUBSCRIPTION '${subId}'"
for line in ${rgs}
do
#echo ${line}
#readarray -t -d $'\t' f <<< $line
IFS=$'\t' read -r -a f <<< $line
echo
echo "+++++++++++++PROCESS RG: "${f[0]}"+++++++++++++"
# skip managed rg and rg in Deleting state
if [ ${f[3]} == 'Deleting' ]; then
echo "SKIP RG UNDER DELETION: "${f[0]}
continue
fi
if [ ${f[4]} != 'None' ]; then
echo "SKIP PRESERVED RG: "${f[0]}
else
activities="$(get-non-audit-activities $subId ${f[0]} $length)"
if [ -z "${activities}" ]; then
echo "NO NON-AUDIT ACTIVITIES IN LAST $length. SHOULD DELETE: ${f[0]}"
cmd="time az group delete --no-wait -y --subscription '${subId}' -n ${f[0]}"
echo $cmd
if [ $realRun == 1 ]; then
eval $cmd || : # Do not exit on error on deletion
fi
else
echo "DETECTED ACTIVITIES: "
echo "${activities}"
fi
fi
done
# reset IFS
IFS=$'\n'