How do you delete an AWS ECS Task Definition?

Once you've created a task definition in Amazon's EC2 Container Service, how do you delete or remove it?


It's a known issue. Once you de-register a Task Definition it goes into INACTIVE state and clutters up the ECS Console.

If you want to vote for it to be fixed, there is an issue on Github. Simply give it a thumbs up, and it will raise the priority of the request.


I've recently found this gist (thanks a lot to the creator for sharing!) which will deregister all task definitions for your specific region - maybe you can adapt it to skip some which you want to keep: https://gist.github.com/jen20/e1c25426cc0a4a9b53cbb3560a3f02d1

You need to have jq to run it: brew install jq

I "hard-coded" my region, for me it's eu-central-1, so be sure to adapt it for your use-case:

#!/usr/bin/env bash
get_task_definition_arns() {
    aws ecs list-task-definitions --region eu-central-1 \
        | jq -M -r '.taskDefinitionArns | .[]'
}

delete_task_definition() {
    local arn=$1

    aws ecs deregister-task-definition \
        --region eu-central-1 \
        --task-definition "${arn}" > /dev/null
}

for arn in $(get_task_definition_arns)
do
    echo "Deregistering ${arn}..."
    delete_task_definition "${arn}"
done

Then when I run it, it starts removing them: Deregistering arn:aws:ecs:REGION:YOUR_ACCOUNT_ID:task-definition/NAME:REVISION...


Oneline approach inspired by Anna A reply:

aws ecs list-task-definitions --region eu-central-1 \
  | jq -M -r '.taskDefinitionArns | .[]' \
  | xargs -I {} aws ecs deregister-task-definition \
        --region eu-central-1 \
        --task-definition {} \
  | jq -r '.taskDefinition.taskDefinitionArn'