Search⌘ K

Terraform Destroy

Explore how to safely destroy resources managed by Terraform using the terraform destroy command. Understand the importance of consistent state files when deleting local and remote resources. Learn the use of terraform plan with the -destroy flag, and how to handle tainted resources to force recreation. Gain practical insights into managing infrastructure lifecycle with Terraform while maintaining state integrity.

Overview

As we know, we can delete Terraform file resources by using rm -rf.

This is fine for simple resources managed within a local directory since the Terraform state file, Terraform module, and the file under Terraform’s control are all in one place. Blitzing them all makes a clean break.

This approach is less viable, however, when we come to manage resources remotely, such as cloud service instances. We can’t manually destroy an EC2 (virtual machine) instance on AWS without getting an inconsistent Terraform state file on our local host. The state file will think the EC2 instance still exists even though we’ve destroyed it manually.

What we want is a simple way to remove the resources under Terraform’s control so that the real world view and the Terraform view are consistent.

...