Ora

How Do You Delete an ECS Service?

Published in AWS ECS Service Management 4 mins read

Deleting an Amazon Elastic Container Service (ECS) service involves a straightforward process within the AWS Management Console, or through programmatic methods, to remove a service that is no longer needed from your cluster. This action stops all tasks managed by the service and removes its configuration from the cluster.

Step-by-Step Guide to Deleting an ECS Service via the AWS Console

The most common way to delete an ECS service is by using the AWS Management Console. Follow these steps:

  1. Log In to the AWS Management Console: Access your AWS account and navigate to the Amazon ECS console.
  2. Navigate to Clusters: In the left-hand navigation pane, select Clusters to view all your ECS clusters.
  3. Select Your Target Cluster: On the Clusters page, choose the specific cluster that contains the service you wish to delete.
  4. Access the Services Tab: Once on the Cluster : your-cluster-name page, select the Services tab. This tab lists all active services within that cluster.
  5. Choose Services for Deletion: Select one or more checkboxes next to the services that you intend to remove.
  6. Initiate Deletion: With the services selected, choose the Delete button, typically located at the top of the services list.
  7. Confirm Deletion and Optional Force Delete: A confirmation dialog will appear.
    • If you need to delete a service even if it still has running tasks (i.e., it wasn't scaled down to zero tasks beforehand), select the Force delete service checkbox.
    • Confirm your decision to proceed with the deletion.

Understanding the "Force Delete Service" Option

The "Force delete service" option is a critical feature to understand during the deletion process.

  • Standard Deletion: By default, Amazon ECS expects services to be scaled down to zero tasks before deletion. If tasks are still running, the standard delete operation might not proceed immediately or could prompt you to scale down first.
  • Force Delete: Selecting Force delete service overrides this requirement. It instructs ECS to immediately stop any remaining tasks associated with the service and then proceed with the deletion. This is particularly useful in scenarios where tasks are stuck, or you need to quickly remove a service without waiting for a graceful shutdown process.

What Happens After an ECS Service is Deleted?

When an ECS service is successfully deleted:

  • Tasks are Stopped: All running tasks that were managed by the deleted service are stopped.
  • Service Configuration Removed: The service's configuration, including its desired count, task definition reference, and networking settings, is removed from the cluster.
  • Associated Resources: While the service itself is removed, associated resources like load balancer target groups or Auto Scaling groups (if manually created or not fully managed by ECS service scheduler) might persist. It's good practice to verify and clean up any orphaned resources if necessary.

Deleting an ECS Service Programmatically (AWS CLI)

For automation or command-line operations, you can use the AWS Command Line Interface (CLI) to delete an ECS service.

aws ecs delete-service --cluster your-cluster-name --service your-service-name

To force delete a service using the CLI:

aws ecs delete-service --cluster your-cluster-name --service your-service-name --force

This method offers flexibility for scripting and integrating service deletion into CI/CD pipelines or operational workflows.

Best Practices for Service Deletion

  • Scale Down First: As a best practice, always try to scale your service's desired count to zero tasks and ensure all tasks have stopped before initiating a deletion, unless you specifically need to use the force delete option.
  • Verify Dependencies: Before deleting, ensure that no other services or applications explicitly depend on the service you are removing.
  • Monitor Cluster State: After deletion, monitor your cluster to ensure all resources are properly released and the cluster operates as expected.

By following these steps and understanding the available options, you can effectively manage the lifecycle of your ECS services, ensuring your container environments remain clean and optimized.