Skip to content

Instantly share code, notes, and snippets.

@suhussai
Created October 21, 2023 20:49
Show Gist options
  • Save suhussai/f891d9142052c5486b75b0d8983a10c2 to your computer and use it in GitHub Desktop.
Save suhussai/f891d9142052c5486b75b0d8983a10c2 to your computer and use it in GitHub Desktop.
Bash script to find and delete auto-generated AWS Lambda, CodeBuild and API Gateway log groups using pagination.
#!/bin/bash
echo "$(date) cleaning up log groups..."
next_token=""
while true; do
if [[ "${next_token}" == "" ]]; then
response=$(aws logs describe-log-groups)
else
response=$(aws logs describe-log-groups --starting-token "$next_token")
fi
log_groups=$(echo "$response" | jq -r '.logGroups[].logGroupName | select(. | test("^/aws/lambda/*|^/aws/codebuild/*|^API-Gateway-Execution-Logs_*"))')
for i in $log_groups; do
echo "$(date) deleting log group with name $i..."
aws logs delete-log-group --log-group-name "$i"
done
next_token=$(echo "$response" | jq '.NextToken')
if [[ "${next_token}" == "null" ]]; then
# no more results left. Exit loop...
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment