# Terminate an instance

Delete an instance's machine and its root disk, and later remove its record from the list.

Replace _i-3f9a2c07_ with your instance's id.

**Warning:**
The machine and its root disk are deleted and cannot be recovered. Take a
[machine image](create-a-machine-image.md) first if you need a copy.

## Prerequisites

- Permission for `ec2:TerminateInstances`.
- In the console, also `ec2:DescribeInstances` to open the instance's page (without it the page is
  not found).
- In the console, the instance is not `pending`, `stopping`, `shutting-down` or `rebooting`; until it
  settles, **Terminate instance** is unavailable with the reason.
- An instance that is a node of a Kubernetes cluster is refused with `409` "This instance is node …
  of the Kubernetes cluster …. Scale or delete its node group instead."

## Terminate

**Console**
1. Open the instance, then choose **Danger zone** (or **Terminate instance** in the **⋯** menu).
2. Under **Terminate this instance**, choose **Terminate instance**.
3. Enter the instance name, then choose **Terminate**.

**CLI**
```sh
dcloud ec2 instances terminate i-3f9a2c07
```

**API**
```http
DELETE /ec2/api/instances/i-3f9a2c07
```

**Terraform**
Remove the `dcloud_instance` resource and run `terraform apply`.

## Result

The instance goes to `shutting-down` while the machine is deleted, then `terminated`. An instance
whose machine did not exist yet goes straight to `terminated`. When it is terminated:

- Volumes made with the instance (`volumes` at launch, with `deleteOnTermination`) are deleted.
  Every other attached volume is detached and becomes **Available**.
- An associated elastic IP is disassociated and stays allocated (and metered) until you release
  it.
- Its security group links are removed.

Instance hours stop with the termination. If deleting the machine fails at the provider, the
instance shows `stopping` with the error on its page; terminate it again with the CLI or API
(the console waits while it is `stopping`).

A machine of an auto scaling group that you terminate here is replaced by the group.

## Remove the record

A terminated instance stays in the list. To remove it, terminate it once more: in the console,
open **Danger zone**, under **Delete this instance record** choose **Delete record**, enter the
name and choose **Delete record**; with the CLI or API, a second `terminate` or `DELETE`. A record already removed answers `400` "Instance record is already deleted".

## Related

- [Instance lifecycle](instance-lifecycle.md)
- `ec2.instances.terminate` · `dcloud ec2 instances terminate` · `dcloud_instance`
