forked from dannysteenman/aws-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_all_inactive_task_definitions.py
38 lines (29 loc) · 1.22 KB
/
delete_all_inactive_task_definitions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# https://github.com/dannysteenman/aws-toolbox
#
# License: MIT
#
# This script deletes all inactive task definitions in the ECS service in all AWS Regions.
import boto3
def get_inactive_task_definition_arns(region):
client = boto3.client("ecs", region_name=region)
response = client.list_task_definitions(status="INACTIVE")
return response.get("taskDefinitionArns", [])
def delete_task_definition(region, arn):
client = boto3.client("ecs", region_name=region)
client.delete_task_definitions(taskDefinitions=[arn])
def delete_inactive_task_definitions_in_all_regions():
ecs_regions = boto3.session.Session().get_available_regions("ecs")
for region in ecs_regions:
try:
arns = get_inactive_task_definition_arns(region)
if not arns:
print(f"No inactive task definitions found in region {region}")
else:
for arn in arns:
print(f"Deleting inactive task definition with ARN: {arn}")
delete_task_definition(region, arn)
except Exception:
print(f"No access to region: {region}")
continue
if __name__ == "__main__":
delete_inactive_task_definitions_in_all_regions()