Remove Activity Logs Diagnostic settings using Az cli

Fri, September 20, 2024 - 1 min read
delete_subscription_activity_logs.sh
 
#!/usr/bin/env sh
 
# List all subscriptions
az account subscription list
 
# Filter the subscriptions that start with dev-, prd-, stg-, can-, liv-, tst-
az account subscription list --query "[?starts_with(displayName, 'dev-') || starts_with(displayName, 'prd-') || starts_with(displayName, 'stg-') || starts_with(displayName, 'can-') || starts_with(displayName, 'liv-') || starts_with(displayName, 'tst-')].{id:subscriptionId, name:displayName}" --output tsv
 
# Save the filtered subscriptions to a variable
subscriptions=$(az account subscription list --query "[?starts_with(displayName, 'dev-') || starts_with(displayName, 'prd-') || starts_with(displayName, 'stg-') || starts_with(displayName, 'can-') || starts_with(displayName, 'liv-') || starts_with(displayName, 'tst-')].{id:subscriptionId, name:displayName}" --output tsv)
 
# Echo only the subscription IDs and save to a new variable
echo "$subscriptions" | awk '{print $1}'
 
# Loop through the subscriptions and delete the diagnostic settings
echo "$subscriptions" | while read -r item; do
  subscription_id=$(echo "$item" | awk '{print $1}')
  # Delete the diagnostic settings for the current subscription
  az monitor diagnostic-settings subscription delete --subscription "$subscription_id" --name "subscriptionToLa" --yes
done