I deleted an Amazon Elastic Container Service (Amazon ECS) task definition, but the task definition is stuck in the DELETE_IN_PROGRESS state.
Resolution
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.
A task definition deletion request can't complete when an Amazon ECS resource depends on the task definition revision. If a standalone task or service uses the task definition that you deleted, then the task definition becomes stuck in the DELETE_IN_PROGRESS state.
To resolve this issue, run the following script to identify all resources that use the task definition:
TaskDefARN=TaskDefinitionARN
REGION=Regioncode
for cluster in $(aws ecs list-clusters --region $REGION --output text --query 'clusterArns[]'); do echo "Checking cluster: $cluster"; for task in $(aws ecs list-tasks --region $REGION --cluster $cluster --output text --query 'taskArns[]'); do task_def=$(aws ecs describe-tasks --region $REGION --cluster $cluster --tasks $task --query 'tasks[].taskDefinitionArn' --output text); if [[ "$task_def" == "$TaskDefARN" ]]; then echo "Task using this definition: $task"; fi; done; for service in $(aws ecs list-services --region $REGION --cluster $cluster --output text --query 'serviceArns[]'); do service_def=$(aws ecs describe-services --region $REGION --cluster $cluster --services $service --query 'services[].taskDefinition' --output text); if [[ "$service_def" == "$TaskDefARN" ]]; then echo "Service using this definition: $service"; fi; done; done
Note: Replace TaskDefinitionARN with the ARN of the task definition and Regioncode with your AWS Region.
Example output:
Checking cluster: arn:aws:ecs:us-east-1:123456789012:cluster/cluster-1
Checking cluster: arn:aws:ecs:us-east-1:123456789012:cluster/cluster-2
Checking cluster: arn:aws:ecs:us-east-1:123456789012:cluster/cluster-3
Task using this definition: arn:aws:ecs:us-east-1:123456789012:task/cluster-3/abcde
Service using this definition: arn:aws:ecs:us-east-1:123456789012:service/cluster-3/service-1
In the preceding example, the abcde task belongs to service-1. To delete the task definition for the abcde task, first delete service-1.
If your service is actively maintaining tasks, then you might receive the following error message when you try to delete the service:
"An error occurred (InvalidParameterException) when calling the DeleteService operation: The service cannot be stopped while it is scaled above 0."
To resolve this issue, verify that the service has no running tasks and that the task count is 0 before you delete the service. Or, add the --force option when you run the delete-service command.
Delete the service from the Amazon ECS console. Or, run the following delete-service AWS CLI command:
aws ecs delete-service --cluster clustername --service servicename
Note: Replace clustername with your cluster name and servicename with your service name.
For Amazon ECS tasks, task definition deletion can take up to 1 hour to complete after you stop the task. For Amazon ECS services, task definition deletion can take up to 24 hours to complete after you delete the deployment or task set.
You can create a new task definition with the same name only after the deletion fully completes. If you didn't delete the task definition yet, then create a new revision instead. Use the revision to launch new tasks until the deletion of the previous task definition completes.
Related information
Amazon ECS task definition states
describe-services
describe-tasks