logo
TerraformDestroy
Terraform

Destroying Everything Terraform Created

Destroying Everything Terraform Created

Terraform has a built-in command for this — terraform destroy. It reads the state file and tears down every resource Terraform manages, in the correct dependency order.


The Command

cd ~/terraform_learn
terraform destroy

Terraform will show you a destruction plan and ask for confirmation before doing anything:

Plan: 0 to add, 0 to change, 4 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: yes

You must type yes exactly and press Enter.


Skip the Confirmation Prompt

If you're sure and want to skip the yes prompt:

terraform destroy -auto-approve

⚠️ Use -auto-approve with caution — there is no undo.


Destroy and Log the Output

terraform destroy -auto-approve 2>&1 | tee destroy.log

Destroy Only a Specific Resource

If you only want to destroy one resource and leave the rest intact:

# Destroy just the VM, keep everything else
terraform destroy -target=oci_core_instance.free_vm

Use the resource names from terraform state list as the target.


What Gets Destroyed in Your Case

Based on your state, running terraform destroy would tear down:

- oci_core_instance.free_vm                 # The VM
- oci_core_subnet.subnet                    # The subnet
- oci_identity_compartment.tf_compartment   # The compartment
- oci_identity_policy.tf_compartment_policy # The IAM policy

💡 Terraform destroys in reverse dependency order — so the VM is destroyed before the subnet, and the subnet before the compartment. It handles this automatically.


Verify Everything is Gone

# Should return an empty state
terraform state list

# Double check the VM is gone in OCI
oci compute instance list \
  --compartment-id ocid1.compartment.oc1..aaaaaaaab7ynu65vmx2dv6tv7rvqg6q4bp7ayx6xbanzxz4ydzyrgwsptlva \
  --config-file /tmp/oci_test_config \
  --profile DEFAULT \
  --auth api_key \
  --query "data[*].{Name:"display-name",State:"lifecycle-state"}" \
  --output table