Delete an ECS task definition using AWS CLI

Scott Coulton profile picture
Scott Coulton
Principal Developer Advocate at AWS

Installation

Download the script below and use chmod to make it executable:

Language: sh
chmod +x delete-tasks.sh

Script

File: delete-tasks.shLanguage: 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