Last active
November 21, 2022 12:59
-
-
Save subudear/3a04fd2eed28ed710da1c7f4eba8744d to your computer and use it in GitHub Desktop.
Delete all log group leaving specific AWS cloudwatch log groups
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Replace the AWS cloudwatch log group name | |
| $log_group_name_to_search = "/aws/lambda/" | |
| # Replace the name of log group or groups which should NOT be DELETED | |
| $log_group_name_not_to_delete = "/aws/lambda/DEV*" | |
| # The below command gets all the log group name matching $log_group_name_to_search. Then display the output is passed script where | |
| # if the log groupname matches $log_group_name_not_to_delete then it is not deleted otherwise deleted. | |
| aws logs describe-log-groups --log-group-name-prefix $log_group_name_to_search --query logGroups --output json | ConvertFrom-json | | |
| ForEach-Object {$_.logGroupName} | ForEach-Object { | |
| if ($_ -like $log_group_name_not_to_delete) {write-host $_ " :NOT Deleted"}` else {Write-Host $_ " : Deleted" | |
| aws logs delete-log-group --log-group-name $_ }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment