TechBuzzPro

Terraform Destroy: Count vs For_Each Checks

· 5 min read · Apps & Browsers

In this article
  1. Background: How Terraform Plans Destroys
  2. Count vs For_Each: Core Differences
  3. What to Check in the Destroy Plan
  4. Common Problems and Misconceptions
  5. JSON Comment Placement in Terraform
  6. This is a valid HCL comment
  7. How to Choose Between Count and For_Each
  8. Safety and Best Practices
  9. Alternatives to Full Destroy
  10. Frequently Asked Questions

When you run terraform destroy, the plan output can differ depending on whether your resources use count or for_each, and JSON comment placement can cause confusing parse errors. The key is to check that your count expression evaluates to a known number at plan time, that your for_each map/set keys are stable, and that any JSON comments are placed outside the jsonencode() string, not inside it.

Background: How Terraform Plans Destroys

Terraform builds a dependency graph from your configuration and state. When you run terraform destroy, it plans the removal of every resource in the state, in reverse dependency order. The plan shows -/+ for replacement or - for pure destruction. The number of resources shown depends on how many instances Terraform knows about from the state, which is determined by the count or for_each meta-arguments.

Count vs For_Each: Core Differences

count creates a fixed number of identical resources, addressed as resource.name[0], [1], etc. for_each creates one resource per key in a map or set, addressed as resource.name["key"]. The destroy plan lists each instance separately. If you change the count or the keys of for_each before running destroy, Terraform will plan to destroy the old instances and create the new ones, not just destroy everything.

What to Check in the Destroy Plan

Before running terraform destroy, review the plan output carefully. Check that:

  • The number of resources to destroy matches your expectation (e.g., if you have 3 instances via count = 3, you should see 3 destroy lines).
  • No resources are shown as + (create) unless you intentionally changed the configuration after the last apply.
  • For for_each, the keys in the destroy plan match the keys in your state. If you removed a key from the map, that instance will be destroyed, but the others will remain.
  • Any count expression that depends on a variable or data source is known before apply; otherwise Terraform may require -target or fail with an "Invalid count argument" error.

Common Problems and Misconceptions

A frequent issue is that count is set to a value that changes after a data source is read. For example, if you use count = length(data.aws_ami.ubuntu.id), the destroy plan may show a mismatch because the data source isn't refreshed during destroy. The fix is to use for_each with a stable set of keys or to hardcode the count for destroy operations.

Another misconception is that terraform destroy ignores configuration changes. It does not; it uses the current configuration to determine what to destroy. If you change count from 2 to 1, destroy will remove one instance, not both. To destroy everything, use terraform destroy without any changes, or run terraform apply -destroy with the original configuration.

JSON Comment Placement in Terraform

Terraform's JSON syntax (.tf.json) does not support comments. If you try to add a // or /* */ comment inside a JSON file, Terraform will fail to parse it. However, you can place comments outside the JSON structure, for example, in a separate .tf file or as a # comment in a .tfvars file. When using jsonencode() in HCL, you must not put comments inside the JSON string; they will be treated as literal characters and may cause validation errors.

If you need to document a JSON configuration, use a separate README or a locals block with a description. For example:

locals {

This is a valid HCL comment

json_config = jsonencode({ "key" = "value" }) }

How to Choose Between Count and For_Each

Use count when you need a simple numeric range of identical resources. Use for_each when you need to manage resources with distinct keys, or when you need to delete/insert specific instances without affecting others. For destroy plans, for_each gives you more granular control because you can remove a key and only that resource is destroyed.

Safety and Best Practices

Always run terraform plan -destroy before actually destroying. Review the plan to ensure no unexpected resources are targeted. Use terraform state list to see all resources in state. If you have resources that should not be destroyed (e.g., a database), use prevent_destroy = true in the resource block. This will cause the destroy plan to fail with an error, protecting the resource.

Alternatives to Full Destroy

If you only need to remove a subset of resources, use terraform destroy -target=resource.name to target specific resources. You can also use terraform apply -destroy -target=... for more control. For temporary environments, consider using terraform workspace to isolate state, and destroy the entire workspace when done.

Frequently Asked Questions

Why does my destroy plan show resources being created?

This happens when your configuration has changed since the last apply. Terraform plans to destroy the old instances and create new ones to match the new configuration. To avoid this, run terraform plan -destroy with the exact configuration that was applied, or use terraform state rm to remove resources from state without destroying them.

Can I use both count and for_each on the same resource?

No, Terraform does not allow both meta-arguments on the same resource block. You must choose one. If you need nested loops, use a for_each with a set of objects that includes a numeric field, or use count with a list of maps.

How do I comment in a .tf.json file?

You cannot add comments inside a .tf.json file. Instead, use a separate .tf file for comments, or use a locals block with HCL comments. If you must use JSON, keep a separate documentation file.

What does "Invalid count argument" mean during destroy?

This error occurs when the count expression cannot be evaluated at plan time, often because it depends on a resource attribute that is unknown until apply. For destroy, use a static count or a value that is known from state.

Is there a way to see what will be destroyed without actually destroying?

Yes, run terraform plan -destroy. This shows the destroy plan without making changes. Review the output carefully, then run terraform destroy to execute.