Destroying the Resources

Learn how to destroy all the resources that we created on the Google Cloud platform.

We'll cover the following

Deleting the resources

We’re (almost) finished with the quick exploration of Terraform using GKE as an example. We’ve seen how to add and change resources, and the only thing missing is to learn how to destroy them.

If we want to delete some of the resources, all we have to do is remove their definitions and execute terraform apply. However, in some cases, we might want to destroy everything. There’s a command for that as well.

We need to keep the storage where the Terraform state is stored. That will allow us to re-create the same cluster for the rest of the exercises. If we use AWS or GCP, we simply use the terraform destroy command because those two don’t allow us to destroy storage if there are files inside it. Or, to be more precise, that’s the default behavior, and we need to specify explicitly that we do want to destroy storage even if it contains files by setting the argument force_destroy to true. However, Azure doesn’t have such a flag. So, if we execute terraform destroy, everything will be gone, including the storage with Terraform state. Since we want to keep that storage, we need to tell Terraform which targets to destroy instead of wiping out everything.

In this case, we do want to destroy all the resources except the storage with Terraform state. We can do that through the --target argument. Fortunately for us, AKS is simple, and the whole cluster is defined as a single resource called azurerm_kubernetes_cluster.

Get hands-on with 1200+ tech skills courses.