Once you've created a task definition in Amazon's EC2 Container Service, how do you delete or remove it?
-
1It is now possible to delete a task definition once you have de-registered. The only thing we need now is deleting empty task definition families. – DBX12 Mar 06 '23 at 08:39
-
@DBX12 task definition families that don't have any task definition revisions will no longer be returned when you call the `list-task-definition-families` API, there's no need to delete empty task definition families – goto Mar 25 '23 at 14:12
-
@goto That is an advancement. I mostly use terraform or the Web UI, so I still see them in the Web UI. So just for "cleaning up completely" you could want that functionality. – DBX12 Mar 28 '23 at 09:55
-
@DBX12 not sure I follow, but assuming you don't have any task definition revisions (`ACTIVE`, `INACTIVE`, or `DELETE_IN_PROGRESS`), then that task definition family won't be returned in the `list-task-definition-families` call. The reason why you might be still seeing that task definition family is because you must have a revision in one of those states/statuses. – goto Mar 29 '23 at 00:28
7 Answers
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.
-
2It seems to be solved now, see the [answer of fedonev](https://stackoverflow.com/a/75589654/11764049) – Aelius Mar 24 '23 at 10:33
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...

- 1,653
- 7
- 10
-
1I don't know why this was getting down-voted, it worked for me. Ok, the INACTIVE definitions stay around, but at least things are deregistered.. – klang Sep 16 '20 at 07:30
-
13I assume because it doesn't do what the OP asked, which is to remove or delete task definitions. – Nick Coad Oct 08 '20 at 01:53
-
-
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-definition task_defination_name:revision_no

- 968
- 10
- 10
-
4If you have x number of revisions you will have to run the above command for every single revision. Once all revisions have been deleted, the task definition is itself deleted. – ajaysinghdav10d Jan 23 '21 at 14:27
-
3This does not delete the Task Definition, it simply deregisters a single version of a Task Definition. Once all versions are deregistered (inactive), the Task Definition itself will also be deregistered but it and all inactive versions remain. It looks like AWS do not actually offer a way to delete Task Definitions, which is disappointing. – David Gard Oct 28 '21 at 15:33
-
I like this solution Just remove all revisions and task definition will disappear from the list But I don't like missing space after --task-definition flag :) – sergpank Feb 27 '23 at 11:17
Feb 2023: Announcing Amazon ECS Task Definition Deletion
The DeleteTaskDefinitions API closes the issue noted in the accepted answer. As previously, deregistering a task definition sets it as inactive. The new API allows you to delete INACTIVE
task definitions:
aws ecs delete-task-definitions --task-definitions family:revision
aws ecs delete-task-definitions --task-definitions myInactiveTaskDef:1 myInactiveTaskDef:2 myInactiveTaskDef:3

- 20,327
- 2
- 25
- 34
-
That's useful. But now how do I get rid of the unused task definition families, which still show up when I remove the `ACTIVE` filter (even though they contain no task definitions)? – Garret Wilson Mar 10 '23 at 22:30
-
1@GarretWilson if all task definition revisions for a task definition are deregistered **and** removed, then the task definition family will also be removed. If your task definition family has any task definition revisions that are `DELETE_IN_PROGRESS`, then the task definition family will be still be returned by the API as `INACTIVE` – goto Mar 21 '23 at 23:43
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'

- 308
- 3
- 13
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.

- 154
- 5
Now its supported I just went inside the Task Definations and clicked on Actions and click on Deregister and it was removed from the UI

- 8,449
- 6
- 31
- 45