How to delete an EC2 instance (AWS Console / CLI)

Updated on
ec2 falling bowling pins

Let’s learn how to delete / terminate Amazon EC2 instances using AWS console and AWS CLI.

Delete EC2 instance via AWS Console

Go to Amazon EC2 console and select Instances in the left sidebar. You will see all the instances which are running with their name and instance state.

Tip

You can remove the Instance state = running filter to see all the other instances as well.

  • Select the instances you want to delete using the checkbox beside the name
  • Click on Instance state drop-down menu
  • Select Terminate instance
  • Click on Terminate

Warning

It is recommended to create a snapshot (backup) of the instance before terminating the instance.

Once you click on Terminate, your instance(s) will be deleted forever.

How to use AWS CLI?

You can use AWS CLI via:

  • AWS CloudShell present in AWS Console
  • AWS CLI installed and set up in your local machine

Get EC2 instance IDs

Find EC2 instance ID using AWS console

Go to Amazon EC2 console, and select Instances in the left sidebar.

You will see all the instances which are running with their Instance ID. You can remove the Instance state = running filter to see all the other instances as well.

AWS console screenshot showing how to get EC2 instance id

Find EC2 instance id using AWS CLI

Run the describe-instances command from your terminal.

aws ec2 describe-instances

You will get the output with all the EC2 instances:

{
    "Reservations": [
        {
            "Groups": [],
            "Instances": [
                {
                    "AmiLaunchIndex": 0,
                    "ImageId": "ami-07fa59ce6da2fe3fe",
                    // Copy this instance id ↓↓↓
                    "InstanceId": "i-0b5fdb14ef35b3efe",
                    "InstanceType": "t4g.micro",
                    "KeyName": "arch-linux",
                    "LaunchTime": "2024-07-11T18:50:17+00:00",
                    "State": {
                        "Code": 16,
                        "Name": "running"
                    },
                    "VpcId": "vpc-b2b3d0cf",
                    "Architecture": "arm64",
                }
            ],
            "OwnerId": "205979422636",
            "ReservationId": "r-025cd5429c07ab191"
        }
    ]
}

Delete EC2 instances with AWS CLI

To terminate one or more EC2 instances, run the terminate-instances command, followed by your instance IDs:

aws ec2 terminate-instances --instance-ids "your-ec2-instance-id" "... more"

Now you can see: the status of the EC2 instance in the CurrentState is shutting-down.

{
    "TerminatingInstances": [
        {
            "CurrentState": {
                "Code": 32,
                "Name": "shutting-down"
            },
            "InstanceId": "i-0b5fdb14ef35b3efe",
            "PreviousState": {
                "Code": 16,
                "Name": "running"
            }
        }
    ]
}

Verify terminated EC2 instances

You can run the same ec2 describe-instances command, and see that the instance state is now terminated.

❯ aws ec2 describe-instances                                      
{
    "Reservations": [
        {
            "Groups": [],
            "Instances": [
                {
                    "AmiLaunchIndex": 0,
                    "ImageId": "ami-07fa59ce6da2fe3fe",
                    "InstanceId": "i-0b5fdb14ef35b3efe",
                    "InstanceType": "t4g.micro",
                    "KeyName": "arch-linux",
                    "LaunchTime": "2024-07-11T18:50:17+00:00",
                    "State": {
                        "Code": 48,
                        "Name": "terminated"
                    },

Show only specific fields from AWS CLI

If you just want to get the list of all instances along with their states, you could use --query option to filter the output.

Just add "Reservations[0].Instances[*].[InstanceId, State.Name]" in the query argument.

You can see now we only see the instance id and the state of that particular instance.

 aws ec2 describe-instances --query "Reservations[0].Instances[*].[InstanceId, State.Name]"  
[
    [
        "i-0b5fdb14ef35b3efe",
        "terminated"
    ]
]

Hills 🏔 and Skills, What's Common?

They both need you to be on top.

You will get lifetime access with:

All yours, just at:

$149

Just type and your search result will magically appear here