Cleaning Up your Amazon ECS Resources
When you are finished experimenting with or using a particular Amazon ECS cluster, you should clean up the resources associated with it to avoid incurring charges for resources that you are not using.
Some Amazon ECS resources, such as tasks, services, clusters, and container instances, are cleaned up using the Amazon ECS console. Other resources, such as Amazon EC2 instances, Elastic Load Balancing load balancers, and Auto Scaling groups, must be cleaned up manually in the Amazon EC2 console or by deleting the AWS CloudFormation stack that created them.
Scale Down Services
If your cluster contains any services, you should first scale down the desired count of tasks in these services to 0 so that Amazon ECS does not try to start new tasks on your container instances while you are cleaning up. Follow the procedure in Updating a Service and enter 0 in the Number of tasks field.
Alternatively, you can use the following AWS CLI command to scale down your service. Be sure to substitute the region name, cluster name, and service name for each service that you are scaling down.
us-west-2ecs update-service --cluster
Before you can delete a cluster, you must delete the services inside that cluster. After your service has scaled down to 0 tasks, you can delete it. For each service inside your cluster, follow the procedures in Deleting a Service to delete it.
Alternatively, you can use the following AWS CLI command to delete your services. Be sure to substitute the region name, cluster name, and service name for each service that you are deleting.
us-west-2ecs delete-service --cluster
Deregister Container Instances
Before you can delete a cluster, you must deregister the container instances inside that cluster. For each container instance inside your cluster, follow the procedures in Deregister a Container Instance to deregister it.
Alternatively, you can use the following AWS CLI command to deregister your container instances. Be sure to substitute the region name, cluster name, and container instance ID for each container instance that you are deregistering.
us-west-2ecs deregister-container-instance --cluster
Delete a Cluster
After you have removed the active resources from your Amazon ECS cluster, you can delete it. Use the following procedure to delete your cluster.
To delete a cluster
Open the Amazon ECS console at https://console.aws.amazon.com/ecs/.
From the navigation bar, select the region that your cluster is in.
In the navigation pane, select Clusters.
On the Clusters page, click the x in the upper-right-hand corner of the cluster you want to delete.
Choose Yes, Delete to delete the cluster.
Alternatively, you can use the following AWS CLI command to delete your cluster. Be sure to substitute the region name and cluster name for each cluster that you are deleting.
us-west-2ecs delete-cluster --cluster
Delete the AWS CloudFormation Stack
If you created your Amazon ECS resources by following the console first-run wizard, then your resources are contained in a AWS CloudFormation stack. You can completely clean up all of your remaining AWS resources that are associated with this stack by deleting it. Deleting the CloudFormation stack terminates the EC2 instances, removes the Auto Scaling group, deletes any Elastic Load Balancing load balancers, and removes the Amazon VPC subnets and Internet gateway associated with the cluster.
To delete the AWS CloudFormation stack
Open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation/.
From the navigation bar, select the region that your cluster was created in.
Select the stack that is associated with your Amazon ECS resources. The Stack Name value starts with
Choose Delete Stack and then choose Yes, Delete to delete your stack resources.