How to delete instance in EC2?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Deleting an instance in Amazon EC2 (Elastic Compute Cloud) is a common task once a virtual machine is no longer needed or its services have been transferred or upgraded. Properly terminating instances ensures that you are not incurring unnecessary costs as AWS charges based on the time duration and resources consumed by your running instances. Below is a detailed guide on how to delete an instance in EC2, complete with technical insights and examples.
Pre-requisites
Before deleting an instance, ensure that:
- Any critical data on the instance is backed up.
- You have appropriate permissions in AWS IAM to delete the instance.
- The termination protection on the instance is disabled.
Steps to Delete an EC2 Instance
Step 1: Login to AWS Management Console
Navigate to the AWS Management Console and search for "EC2" in the services search bar to open the EC2 Dashboard.
Step 2: Select Instances
In the EC2 Dashboard, click on "Instances" in the left-hand navigation pane to view all your EC2 instances.
Step 3: Choose Instance to Delete
From the list of running instances, select the instance that you want to terminate by checking the checkbox next to it.
Step 4: Disable Termination Protection (if enabled)
Before terminating, ensure that termination protection is not enabled for the instance. You can do this by:
- Selecting "Actions" > "Instance Settings" > "Change Termination Protection".
- Uncheck the "Enable" option if it is checked.
Step 5: Terminate the Instance
With the instance selected, go to:
- "Actions" > "Instance State" > "Terminate Instance".
- Confirm the termination when prompted. AWS will then proceed to terminate the selected instance.
Step 6: Verify Instance Termination
You can verify the termination by:
- Observing the instance state change from "Running" to "Shutting-down" and finally to "Terminated".
Below is a table summarizing the steps involved:
| Step | Action | Details |
| 1 | Login | Access AWS Management Console. |
| 2 | Navigate | Go to EC2 Dashboard > Instances. |
| 3 | Select | Choose the instance(s) to terminate. |
| 4 | Disable Termination Protection | Ensure protection settings are disabled. |
| 5 | Terminate | Use instance actions to confirm termination. |
| 6 | Verify | Check instance state to ensure termination. |
Additional Considerations
Data Preservation
Data stored on instance store volumes is lost when the instance is terminated. Ensure you have copied any critical data to an external location or use AWS Elastic Block Store (EBS) volumes with snapshots.
Billing Implications
Billing for EC2 instances stops once the instance is terminated. However, any associated resources (like Elastic IPs, reserved snapshots, etc.) may continue to incur charges.
Clean-up Resources
After an instance is terminated, remember to check and clean up associated AWS resources such as Security Groups, Elastic Load Balancers, and EBS volumes if no longer needed.
Scripting Instance Termination
AWS EC2 instances can also be terminated programmatically using AWS CLI or SDKs in supported languages (e.g., Python with Boto3).
Here's an example using the AWS CLI:
Or using Python and Boto3:
Both examples assume you have configured your AWS credentials and have the necessary permissions to execute these commands.
By following these steps and recommendations, you can effectively and efficiently terminate EC2 instances while managing costs and preserving essential data.
Related reading
- How to Delete Kubernetes Service
- How to delete multiple files in S3 bucket with AWS CLI
- How to delete multiple rows in DynamoDB?
- How to delete non empty s3 bucket with terraform?
- How to delete untagged images from AWS ECR Container Registry
- How to deploy a React NodeJS Express application to AWS?
- How to deploy TURN servercoturn inside Kubernetes
- How to describe AWS Lambda function test events in CloudFormation template?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.