# Destroy Kubernetes Cluster

> - **Objective:** Learn to destroy the cluster and revoke all related resources.
> - **Prerequisites:**
> - **Optionality:** Required
> - **Estimated time:** 15min

## Destroy resources using Terraform

> - **Optionality:** Required

Before you destroy the cluster, you should clean the kubernetes resources such as LoadBalancer and PersistentVolumes. Then, You can simply use the `terraform destroy` command, and Terraform will be responsible for releasing all the resources it previously applied for.

Note that if you've added any additional resources, such as an EC2 instance in the same VPC as your Kubernetes cluster, that can prevent Terraform from successfully destroying the resources.

If you encounter any problems in using Terraform to destroy the cluster, as an alternative, you can always skip this section and handle it through the AWS web page.

### Command

```{.bash .copyable}
terraform destroy
```

```{.output}
Plan: 0 to add, 0 to change, 62 to destroy.
...
Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value:
```

You will need to enter `yes` to proceed. The installation process will continue for a while. If successful, you will eventually see this output:

```{.output}
Destroy complete! Resources: 62 destroyed.
```

### Troubleshooting

#### Error: error deleting subnet

If you get an error like `Error: error deleting subnet (subnet-0536378d0b54b33cf): timeout while waiting for state to become 'destroyed' (last state: 'pending', timeout: 20m0s)`, you should [manually remove the VPC](#delete-vpc) using the AWS console.

## Destroy resources through AWS web page

> - **Optionality:** Optional

If you encounter any problems when you use Terraform to destroy the cluster, or if you lose the Terraform environment, or after verify and find any residual resources, you can use the following steps to clean up manually. It's important to follow these steps in the order they're given, because of the way dependencies work in AWS.

### Delete Auto Scaling Group

Open the AWS EC2 Auto Scaling page in your browser; replace `{region}` with the AWS region you are using: <https://console.aws.amazon.com/ec2/autoscaling/home?region={region}#AutoScalingGroups:view=details>

Click "*Auto Scaling Groups*" in the TreeView on the left side. Select all items belonging to `my-cluster` and delete them.

Click "*Launch Configurations*" in the TreeView on the left side. Select all items belonging to `my-cluster` and delete them.

### Delete EC2 instances

Open the EC2 Instances page in your browser; replace `{region}` with the AWS region you are using: <https://console.aws.amazon.com/ec2/v2/home?region={region}#Instances:>

If there are any instances belonging to `my-cluster` and the instance state is not "*terminated*", select them all and choose "*Actions*" -> "*Instance State*" -> "*Terminate*".

### Delete EBS volumes

Open the Volumes management page in your browser; replace `{region}` with the AWS region you are using: <https://console.aws.amazon.com/ec2/v2/home?region={region}#Volumes>

If there are any volumes belonging to `my-cluster`, delete them.

### Delete Load Balancers

Open the Load Balancer management page in your browser; replace `{region}` with the AWS region you are using: <https://console.aws.amazon.com/ec2/v2/home?region={region}#LoadBalancers>

If there are any items belonging to `my-cluster`, delete them.

### Delete EKS

Open the AWS EKS management page in your browser; replace `{region}` with the AWS region you are using: <https://console.aws.amazon.com/eks/home?region={region}>

If there is an item named `my-cluster`, delete it.

### Delete VPC

Open the AWS VPC management page in your browser; replace `{region}` with the AWS region you are using: <https://console.aws.amazon.com/vpc/home?region={region}#vpcs>

If there is a item named `my-cluster`, delete it. It is possible that there are some in-use network interfaces or NAT gateways, you can just follow the promoted links to clean up.

## Confirm resources released on AWS

> - **Optionality:** Optional

In order to prevent abnormalities and cause unnecessary costs, we strongly recommend to double-check on the AWS platform that all resources have been deleted.

### Ensure the VPC is released

Open the AWS VPC management page in your browser; replace `{region}` with the AWS region you are using: <https://console.aws.amazon.com/vpc/home?region={region}#vpcs>

Make sure there is no `my-cluster` in the list because it should be removed already.

### Ensure the EBS (block store) volumes are released

Open the Volumes management page in your browser; replace `{region}` with the AWS region you are using: <https://console.aws.amazon.com/ec2/v2/home?region={region}#Volumes>

If there are any volumes belonging to `my-cluster`, delete them.

## Clean up local Terraform state file

> - **Optionality:** Optional

If you are trying to recover from an anomaly, and preparing to re-run the experiment after manually deleting the cluster, you need to remove the local Terrraform state file before you can run Terraform again.

```{.bash .copyable}
rm terraform.tfstate
```
