Skip to content

Instantly share code, notes, and snippets.

@jseiser
Last active February 25, 2021 14:39
Show Gist options
  • Save jseiser/98a043f5176f4ccaf637f2d89e3c842b to your computer and use it in GitHub Desktop.
Save jseiser/98a043f5176f4ccaf637f2d89e3c842b to your computer and use it in GitHub Desktop.
Delete Empty Cloudwatch Log Groups
#!/usr/bin/env python3
import botocore
from botocore.config import Config
import boto3
config = Config(retries=dict(max_attempts=10))
client = boto3.client("logs", config=config)
paginator = client.get_paginator("describe_log_groups")
iterator = paginator.paginate()
for page in iterator:
for i in page["logGroups"]:
print(
f"LogGroup: {i['logGroupName']} - StoredBytes:{i['storedBytes']}"
)
if i["storedBytes"] == 0:
print(f"Deleting: {i['logGroupName']} - {i['storedBytes']}")
try:
response = client.delete_log_group(
logGroupName=i["logGroupName"],
)
except botocore.exceptions.ClientError as error:
if error.response["Error"]["Code"] == "LimitExceededException":
logger.warn(
"API call limit exceeded; backing off and retrying..."
)
else:
raise error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment