How do you delete an AWS ECS Task Definition?

Amazon Web-ServicesContainersAmazon Ecs

Amazon Web-Services Problem Overview


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

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

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.

Solution 2 - Amazon Web-Services

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...

Solution 3 - Amazon Web-Services

There is no option to delete a task definition on the AWS console.

But, you can deregister (delete) a task definition by executing the following command number of revisions that you have:

aws ecs deregister-task-definition --task-definitiontask_defination_name:revision_no

Edit: Edited after comments to more understanding

Solution 4 - Amazon Web-Services

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'

Solution 5 - Amazon Web-Services

Created following gist to safely review, filter and deregister AWS task-definitions and revisions in bulk (max 100 at a time) using JS CLI.

https://gist.github.com/shivam-nagar/aa79b02b74f616f8714d51e419bd10de

Can use this to deregister all revisions for task-definition. This will result in task-definition itself marked as inactive.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwjimenez5271View Question on Stackoverflow
Solution 1 - Amazon Web-ServicesgbodaView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesAnna GellerView Answer on Stackoverflow
Solution 3 - Amazon Web-ServiceshbceylanView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesIgor PchelkoView Answer on Stackoverflow
Solution 5 - Amazon Web-ServicesShivam NagarView Answer on Stackoverflow