Skip to content

Instantly share code, notes, and snippets.

@woutercouvaras
Last active February 22, 2024 12:55
Show Gist options
  • Save woutercouvaras/a466afc9af65ff9d798b8401f19db6e0 to your computer and use it in GitHub Desktop.
Save woutercouvaras/a466afc9af65ff9d798b8401f19db6e0 to your computer and use it in GitHub Desktop.
Script to delete ecs tasks, clusters and services
#!/usr/bin/env bash
# inspired by:
# https://gist.github.com/jen20/e1c25426cc0a4a9b53cbb3560a3f02d1
get_cluster_arns() {
aws ecs list-clusters --region sa-east-1 \
--output json \
| jq -M -r '.clusterArns | .[]'
}
get_service_arns() {
local arn=$1
echo $arn
aws ecs list-services --region sa-east-1 \
--cluster "${arn}" \
--output json \
| jq -M -r '.serviceArns | .[]'
}
delete_services() {
local arn=$1
for service_arn in $(get_service_arns "${arn}")
do
echo "Deleting service ${arn}..."
aws ecs delete-service \
--region sa-east-1 \
--cluster "${arn}" \
--service "${service_arn}" \
--output json \
--force > /dev/null
done
}
delete_cluster() {
local arn=$1
aws ecs delete-cluster \
--region sa-east-1 \
--cluster "${arn}" > /dev/null
}
for arn in $(get_cluster_arns)
do
echo "Deleting services ${arn}..."
delete_services "${arn}"
echo "Deleting cluster ${arn}..."
delete_cluster "${arn}"
done
#!/usr/bin/env bash
# inspired by:
# https://gist.github.com/jen20/e1c25426cc0a4a9b53cbb3560a3f02d1
get_task_definition_arns() {
aws ecs list-task-definitions \
--region sa-east-1 \
--output json \
--status "INACTIVE" | jq -M -r '.taskDefinitionArns | .[]'
}
delete_task_definition() {
local arn=$1
echo $arn
aws ecs delete-task-definitions \
--region sa-east-1 \
--task-definitions "${arn}" > /dev/null
}
# if [ -z "${AWS_REGION}" ] ; then
# (>&2 echo "AWS_REGION is not set")
# exit 1
# fi
for arn in $(get_task_definition_arns)
do
echo "Deleting ${arn}..."
delete_task_definition "${arn}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment