|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import concurrent.futures |
| 3 | +import time |
| 4 | +from typing import List |
| 5 | + |
| 6 | +import boto3 |
| 7 | +from botocore.config import Config |
| 8 | + |
| 9 | + |
| 10 | +def delete_log_group(log_group_name: str) -> str: |
| 11 | + """Delete a single log group""" |
| 12 | + try: |
| 13 | + config = Config( |
| 14 | + read_timeout=30, connect_timeout=10, retries={"max_attempts": 2} |
| 15 | + ) |
| 16 | + client = boto3.client("logs", config=config) |
| 17 | + client.delete_log_group(logGroupName=log_group_name) |
| 18 | + return f"✓ Deleted: {log_group_name}" |
| 19 | + except Exception as e: |
| 20 | + return f"✗ Failed to delete {log_group_name}: {str(e)}" |
| 21 | + |
| 22 | + |
| 23 | +def get_idp_log_groups() -> List[str]: |
| 24 | + """Get all log groups with 'LMA' in the name""" |
| 25 | + config = Config(read_timeout=30, connect_timeout=10, retries={"max_attempts": 2}) |
| 26 | + client = boto3.client("logs", config=config) |
| 27 | + log_groups = [] |
| 28 | + next_token = None |
| 29 | + page_count = 0 |
| 30 | + |
| 31 | + print("Scanning for log groups...") |
| 32 | + while True: |
| 33 | + try: |
| 34 | + page_count += 1 |
| 35 | + print(f"Processing page {page_count}...") |
| 36 | + |
| 37 | + kwargs = {"limit": 50} |
| 38 | + if next_token: |
| 39 | + kwargs["nextToken"] = next_token |
| 40 | + |
| 41 | + response = client.describe_log_groups(**kwargs) |
| 42 | + |
| 43 | + page_idp_count = 0 |
| 44 | + for log_group in response["logGroups"]: |
| 45 | + if "LMA" in log_group["logGroupName"]: |
| 46 | + log_groups.append(log_group["logGroupName"]) |
| 47 | + page_idp_count += 1 |
| 48 | + |
| 49 | + print( |
| 50 | + f"Page {page_count}: {page_idp_count} idp- groups, {len(log_groups)} total so far" |
| 51 | + ) |
| 52 | + |
| 53 | + if "nextToken" not in response: |
| 54 | + print("Reached end of log groups") |
| 55 | + break |
| 56 | + next_token = response["nextToken"] |
| 57 | + |
| 58 | + except Exception as e: |
| 59 | + print(f"Error on page {page_count}: {e}") |
| 60 | + break |
| 61 | + |
| 62 | + return log_groups |
| 63 | + |
| 64 | + |
| 65 | +def main(): |
| 66 | + log_groups = get_idp_log_groups() |
| 67 | + |
| 68 | + if not log_groups: |
| 69 | + print("No log groups found with 'idp-' in the name.") |
| 70 | + return |
| 71 | + |
| 72 | + print(f"\nTotal found: {len(log_groups)} log groups") |
| 73 | + |
| 74 | + confirm = input(f"Delete all {len(log_groups)} log groups? (y/N): ") |
| 75 | + if confirm.lower() != "y": |
| 76 | + print("Cancelled.") |
| 77 | + return |
| 78 | + |
| 79 | + # Process in batches of 50 |
| 80 | + batch_size = 50 |
| 81 | + total_deleted = 0 |
| 82 | + total_failed = 0 |
| 83 | + |
| 84 | + for i in range(0, len(log_groups), batch_size): |
| 85 | + batch = log_groups[i : i + batch_size] |
| 86 | + batch_num = (i // batch_size) + 1 |
| 87 | + |
| 88 | + print( |
| 89 | + f"Processing batch {batch_num}/{(len(log_groups) + batch_size - 1) // batch_size} ({len(batch)} log groups)..." |
| 90 | + ) |
| 91 | + |
| 92 | + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor: |
| 93 | + future_to_name = { |
| 94 | + executor.submit(delete_log_group, name): name for name in batch |
| 95 | + } |
| 96 | + |
| 97 | + for future in concurrent.futures.as_completed(future_to_name, timeout=60): |
| 98 | + result = future.result() |
| 99 | + if result.startswith("✓"): |
| 100 | + total_deleted += 1 |
| 101 | + else: |
| 102 | + total_failed += 1 |
| 103 | + print(result) |
| 104 | + |
| 105 | + print( |
| 106 | + f"Batch {batch_num} complete: {total_deleted} total deleted, {total_failed} total failed" |
| 107 | + ) |
| 108 | + time.sleep(0.5) |
| 109 | + |
| 110 | + print(f"\nFinal results: {total_deleted} deleted, {total_failed} failed") |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments