Why can't I delete my Amazon ECS cluster?
I can't delete my Amazon Elastic Container Service (Amazon ECS) cluster.
Short description
When you try to delete your cluster, you might receive one of the following error messages:
- "Error occurred during operation 'DeleteClusters SDK Error: The Cluster cannot be deleted while Tasks are active. (Service: AmazonECS; Status Code: 400; Error Code: ClusterContainsTasksException; Request ID: 144948ca-1614-40e7-a08b-80d7f1fe38f6; Proxy: null)'." (RequestToken: f389c9fe-8ce3-855c-581a-ebe32a9a9901, HandlerErrorCode: GeneralServiceException)"
- "Error occurred during operation 'DeleteClusters SDK Error: The Cluster cannot be deleted while Services are active. (Service: AmazonECS; Status Code: 400; Error Code: ClusterContainsServicesException; Request ID: 1eb29389-dab7-4345-a453-cde2c2b54c60; Proxy: null)'." (RequestToken: ec646d54-de77-f96c-d9a6-ac04c520f45a, HandlerErrorCode: GeneralServiceException)"
- "Error: deleting ECS Cluster (arn:aws:ecs:REGION:000000000000:cluster/my-ecs-cluster): operation error ECS: DeleteCluster, https response error StatusCode: 400, RequestID: 128b7de0-45e3-4130-8c8c-dcceb27a8bb9, ClusterContainsContainerInstancesException: The Cluster cannot be deleted while Container Instances are active or draining."
- "An error occurred (UpdateInProgressException) when calling the DeleteCluster operation: The specified cluster is in a busy state. Cluster attachments must be in UPDATE_COMPLETE or UPDATE_FAILED state before they can be updated. Wait and try again."
To delete an Amazon ECS cluster, you must first delete all services, stop all running tasks, deregister all container instances, and delete all namespaces.
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.
(Optional) Identify the Amazon ECS service namespaces
Before you delete the service and tasks, it's a best practice to identify the non-essential AWS Cloud Map namespace that's associated with your service.
If your service uses service discovery, then run the following describe-services AWS CLI command to identify the service discovery service ARN:
aws ecs describe-services --cluster CLUSTER_NAME --service SERVICE_ARN --query services[*].serviceRegistries[*].registryArn --output text
Note: Replace CLUSTER_NAME with the cluster name and SERVICE_ARN with the Amazon ECS service ARN.
Then, run the following get-service command to identify the namespace ID:
aws servicediscovery get-service --id SERVICE_ID --query Service.NamespaceId --output text
Note: Replace SERVICE_ID with the service discovery service ID.
If your service uses Service Connect, then run the following describe-services command to identify the namespace ID:
aws ecs describe-services --cluster CLUSTER_NAME --service SERVICE_ARN --query services[*].deployments[].serviceConnectConfiguration.namespace --output text
Note: Replace CLUSTER_NAME with the cluster name and SERVICE_ARN with the Amazon ECS service ARN.
Stop all the running tasks
To list all running tasks, run the following list-tasks command:
aws ecs list-tasks --cluster CLUSTER_NAME --region REGION
Note: Replace CLUSTER_NAME with the cluster name, and REGION with your AWS Region.
To stop a task, run the following stop-task command:
aws ecs stop-task --cluster CLUSTER_NAME --task TASK_ARN --region REGION
Note: Replace CLUSTER_NAME with the cluster name, TASK_ARN with the task ARN, and REGION with your Region.
Or, use the Amazon ECS console to stop the task.
Delete the Amazon ECS services
To list all services in the cluster, run the following list-services command:
aws ecs list-services --cluster CLUSTER_NAME
Note: Replace CLUSTER_NAME with the cluster name.
To delete a service, run the following delete-service command:
aws ecs delete-service --cluster CLUSTER_NAME --service SERVICE_ARN --force --region REGION
Note: Replace CLUSTER_NAME with the cluster name, SERVICE_ARN with the service ARN, and REGION with your Region.
Or, use the Amazon ECS console to delete the service.
Deregister the Amazon ECS container instances
To list all registered container instances, run the following list-container-instances command:
aws ecs list-container-instances --cluster CLUSTER_NAME --region REGION --output text --query containerInstanceArns
Note: Replace CLUSTER_NAME with the cluster name, and REGION with your Region.
To deregister the container instance from the cluster, run the following deregister-container-instance command:
aws ecs deregister-container-instance --cluster CLUSTER_NAME --region REGION --container-instance CONTAINER_INSTANCE_ARN --force
Note: Replace CLUSTER_NAME with the cluster name, REGION with your Region and CONTAINER_INSTANCE_ARN with container instance ARN.
Or, use the Amazon ECS console to deregister the container instance.
Delete the namespace
Deregister all service instances
To list all service discovery instances in a service, run the following list-instances command:
aws servicediscovery list-instances --service-id SERVICE_DISCOVERY_ID_EXAMPLE
Note: Replace SERVICE_DISCOVERY_ID_EXAMPLE with the service discovery service ID.
To deregister the instance from the service discovery service, run the following deregister-instance command:
aws servicediscovery deregister-instance --service-id SERVICE_DISCOVERY_ID_EXAMPLE --instance-id INSTANCE_EXAMPLE
Note: Replace SERVICE_DISCOVERY_ID_EXAMPLE with the service discovery service ID and INSTANCE_EXAMPLE with the instance ID.
Delete all service discovery services
To view all the service discovery services in the namespace, run the following list-services command:
aws servicediscovery list-services --filters Name=NAMESPACE_ID,Values=NAMESPACE_EXAMPLE,Condition=EQ
Note: Replace NAMESPACE_EXAMPLE with your namespace ID.
To delete a service discovery service, run the following delete-service command:
aws servicediscovery delete-service --id SERVICE_ID_EXAMPLE
Note: Replace SERVICE_ID_EXAMPLE with the service ID.
Delete the namespace
To delete the namespace, run the following delete-namespace command:
aws servicediscovery delete-namespace --id NAMESPACE_EXAMPLE
Note: Replace NAMESPACE_EXAMPLE with your namespace ID.
Or, use the AWS Cloud Map console to delete the namespace.
Check the cluster attachments status
If you associated your cluster with an Amazon EC2 Auto Scaling group capacity provider, then check the status of the cluster attachments. The cluster attachments must have the UPDATE_COMPLETE or UPDATE_FAILED status before you delete the cluster. If you delete a cluster with cluster attachments that have the UPDATE_IN_PROGRESS status, then you receive the UpdateInProgressException error.
To check your cluster attachments, run the following describe-clusters command:
aws ecs describe-clusters --cluster CLUSTER_NAME --include ATTACHMENTS --query clusters\[\].attachmentsStatus
Note: Replace CLUSTER_NAME with the cluster name.
If the cluster attachments have the UPDATE_IN_PROGRESS status, then wait until the status automatically resolves before you delete the cluster.
(Optional) Delete the capacity providers associated with the cluster
To view the capacity providers that are associated with the cluster, run the following describe-clusters command:
aws ecs describe-clusters --cluster CLUSTER_NAME --include ATTACHMENTS --query clusters\[\].capacityProviders[] --output text
Note: Replace CLUSTER_NAME with the cluster name.
To disassociate the capacity provider from the cluster, run the following put-cluster-capacity-providers command:
aws ecs put-cluster-capacity-providers --cluster CLUSTER_NAME --capacity-providers [] --default-capacity-provider-strategy []
Note: Replace CLUSTER_NAME with the cluster name.
To delete the capacity provider, run the following delete-capacity-provider command:
aws ecs delete-capacity-provider --capacity-provider CAPACITY_PROVIDER_NAME
Note: Replace CAPACITY_PROVIDER_NAME with the capacity provider name.
Or, use the Amazon ECS console to delete the capacity provider.
Delete the cluster
To delete the cluster, run the following delete-cluster command:
aws ecs delete-cluster --cluster CLUSTER_NAME
Note: Replace CLUSTER_NAME with the cluster name.
Or, use the Amazon ECS console to delete the cluster.
Related information
- Topics
- Containers
- Language
- English

Relevant content
asked 2 years ago
asked 3 months ago