A terraform destroy that took out resources you never meant to touch is almost never Terraform going rogue. It is Terraform doing exactly what the plan told it to do, against a workspace, a state file, or a resource address that was not the one you had in your head.
I have watched this happen twice in anger and helped clean up after several more. The pattern is consistent. Nobody typed terraform destroy at production on purpose. They ran it against what they believed was a throwaway environment, the apply came back green, and the pager went off ninety seconds later. The cause is one of three things, and all three are visible in the plan output before you ever confirm. This is how to trace the near-miss before it becomes the incident.
Cause one: the wrong workspace or the wrong state
Terraform CLI workspaces are the classic foot-gun. A single configuration backs multiple state files, and the only thing telling you which one you are pointed at is terraform workspace show — which nobody runs before a destroy. You think you are in dev; you are in default, which someone quietly wired to production state months ago.
Free · 4 minutes
Is your engineering team shipping safely, or quietly accumulating risk?
Fourteen questions on how work gets from idea to production — cadence, testing, rollback, and the key-person risk in your delivery. Banded finding on screen, full sheet by email.
The shared-state variant is worse. Two configurations, or two engineers, pointed at the same remote backend key. One runs a destroy scoped to what they think is their slice, and because the state is shared, the plan reaches resources the other person owns. The state file, not the directory you are standing in, defines the blast radius.
Confirm which state you are about to mutate before anything else:
# Which workspace, and what does its state actually contain?
terraform workspace show
terraform state list | head
# Where is that state stored? Read the backend block, do not assume.
terraform state pull | jq '.terraform_version, (.resources | length)'
If terraform state list returns resources you do not recognise, stop. You are pointed at someone else’s estate. This is the same discipline as knowing exactly what is in an inventory before you act on it — the argument I made for a bill of materials for models and training data applies just as hard to infrastructure state.
Cause two: a changed for_each key, read as delete-and-recreate
This one destroys resources without anybody running destroy at all. Terraform tracks every for_each resource by its map key, not by position. Change the key and Terraform does not see an edit — it sees the old key vanish and a new key appear, so it plans to destroy the old instance and create a fresh one.
# Before: keyed by a stable, meaningful string
resource "aws_db_instance" "tenant" {
for_each = {
acme = "db.t3.medium"
globex = "db.t3.large"
}
identifier = "tenant-${each.key}"
instance_class = each.value
}
# After: someone "tidies up" and keys by index instead.
# for_each = { for i, t in var.tenants : i => t.size }
# Now the keys are 0, 1 instead of acme, globex —
# every instance is destroyed and recreated under a new address.
A renamed tenant, a switch from count to for_each, a refactor that keys off list index instead of a stable identifier — each one silently rewrites every address in the set. For a database or a stateful volume, destroy-and-recreate means data loss, not a blip. The rule is simple and non-negotiable: the for_each key must be a stable identifier that never changes for the life of the resource. Treat the key as part of the resource’s identity, the same way you would treat an API contract as fixed once published — the contract-first discipline is the same instinct.
Cause three: -target that went wider than you thought
The -target flag feels surgical. It is not. Terraform targets the resource you named and everything that depends on it, because it has to, to keep the graph consistent. Target a security group that ten services reference and the plan pulls those ten services in too. Under a destroy, that dependency fan-out is the mechanism that turns a scalpel into a chainsaw.
HashiCorp itself documents -target as an escape hatch for recovering from mistakes, not a routine workflow. If you are reaching for it on a normal change, the module boundaries are wrong. This matters most for shared infrastructure — the security groups, load balancers and internal mTLS trust anchors that half the estate depends on and nobody expects to disappear.
Read the plan — it already told you
Every one of these is visible in terraform plan before you confirm. The problem is that people read the summary line and skim the body. Do the opposite: read the destroy lines specifically.
# Save the plan, then inspect only what is being destroyed.
terraform plan -out=tfplan
terraform show -json tfplan \
| jq -r '.resource_changes[]
| select(.change.actions | index("delete"))
| .address'
# Zero lines is what a safe change looks like.
# Any address here is a resource that will cease to exist.
Wire that into CI so a plan producing any delete action fails the pipeline unless a human explicitly approves it. The -/+ replace marker and the - destroy marker are the two lines that matter; a green summary hides both.
The guardrail that actually holds
For the resources you can never afford to lose — production databases, state buckets, KMS keys — add a prevent_destroy lifecycle block. If any plan would destroy that resource, Terraform errors out instead of proceeding, whatever the cause.
resource "aws_db_instance" "prod" {
identifier = "core-prod"
# ... other arguments ...
lifecycle {
prevent_destroy = true
}
}
Two things to know. First, prevent_destroy must be a literal true — you cannot drive it from a variable, because lifecycle meta-arguments are evaluated too early for that. Second, it blocks destroy and replacement, but it does not block someone deleting the block itself or running terraform state rm. It is a tripwire, not a lock. It buys you the one thing that matters in the moment: Terraform refuses to proceed, and you get to think.
If the real thing has already happened, do not panic-apply. If you kept remote state versioning on your backend — and you should — the pre-destroy state is one version back; restore it and re-import the resources that still exist in the cloud but not in state. If you did not, the provider’s own backups (RDS automated snapshots, S3 versioning) are your recovery path, and the reconstruction is manual. Which is the whole argument for the guardrails: recovery from a partial destroy costs hours and nerve, and reading three lines of a plan costs thirty seconds.
Terraform did not take out the wrong resources. It took out exactly the resources the plan named — and the plan was open in front of you the whole time.
Build and rescue work
Hands-on delivery of this kind is handled by Sixteen Pillars Studio.
Free interactive tool
Website compliance checklist
What your site has to do, based on what it actually does
Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.
Everything that applies
Ordered by what to do first: legal requirements you can close quickly, then larger pieces of work, then what is expected rather than required. Not exhaustive, and not a legal audit.
Dated PDF, yours to keep or circulate.
Most technology problems are not technology problems. They are control problems.
The systems exist. The investment has been made. The question is whether leadership can understand, direct, evidence, and sustain what those systems produce. Find out where control exists — and where it only appears to.