AWS Builder Center

Delete an ECS task definition using AWS CLI

A bash script for deleting ECS task definitions using the AWS CLI

Installation

Download the script below and use chmod to make it executable:
1
chmod +x delete-tasks.sh

Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File: delete-tasks.sh
#!/bin/bash -ex

TASKNAME=<task name>
START=1 # the first number of the task revision to loop through
END=1000 # The last number to stop the delete loop at

# This function will deregister the task definition
for (( x=$START; x<=$END; x++ ))
do
aws ecs deregister-task-definition --task-definition $TASKNAME:$x --no-cli-pager
sleep 5
echo "The task $TASKNAME and revision $x has been deregistered"
done

# This function will delete the task definition
for (( y=$START; y<=$END; y++ ))
do
aws ecs delete-task-definitions --task-definitions $TASKNAME:$y --no-cli-pager
sleep 5
echo "The task $TASKNAME and revision $y has been deleted"
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File: delete-tasks.sh
#!/bin/bash -ex

TASKNAME=<task name>
START=1 # the first number of the task revision to loop through
END=1000 # The last number to stop the delete loop at

# This function will deregister the task definition
for (( x=$START; x<=$END; x++ ))
do
aws ecs deregister-task-definition --task-definition $TASKNAME:$x --no-cli-pager
sleep 5
echo "The task $TASKNAME and revision $x has been deregistered"
done

# This function will delete the task definition
for (( y=$START; y<=$END; y++ ))
do
aws ecs delete-task-definitions --task-definitions $TASKNAME:$y --no-cli-pager
sleep 5
echo "The task $TASKNAME and revision $y has been deleted"
done

Usage

Modify the following variables to use the script:
  • TASKNAME - The task definition family to delete from. Use aws ecs list-task-definitions to list task definitions if you are unsure.
  • START - The revision number to start deleting from
  • END - The revision number to stop deleting at
Note that task definitions are 1 based, not zero based, so to delete the first 1000 task definition revisions set START=1 and END=1000
ℹ️ Info: Deleting task definitions may take a while. The task definitions will not be instantly deleted. Instead they will transition to DELETE_IN_PROGRESS state and ECS will gradually clean up these task definitions in the background when they are no longer in use by any ECS services or running tasks.

See also

Watch a video of how to delete task definitions in the console:
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article