Skip to content

OpenTofu: Resource instance cannot be destroyed (prevent_destroy)

IaCtested on OpenTofu v1.13.2, Docker-Provider

lifecycle { prevent_destroy = true } protects a resource in the code. If a plan would delete it, OpenTofu aborts the entire run – nothing is deleted, not even other resources.

Symptom

tofu destroy -auto-approve -no-color 2>&1 | grep -E '^Error|prevent_destroy set'
Error: Resource instance cannot be destroyed
Resource instance docker_volume.db has prevent_destroy set, but the plan

The database volume should survive a destroy – that is why the code protects it.

Diagnosis

Check what the aborted run did – nothing.

docker ps --filter name=notes- --format '{{.Names}}' | wc -l
6

Causes

prevent_destroyProtection in the lifecycle block

The block lifecycle { prevent_destroy = true } on docker_volume.db forbids any plan that deletes this resource.

whole runOpenTofu rejects everything

Not only the protected resource stays – the whole run aborts, all six containers keep running.

Fix

Decide on purpose. If the resource should stay, remove only the other resources in a targeted way (destroy -target). If it really has to go, remove the lifecycle block in the code and check the plan again.

Don't: Treat prevent_destroy as a backup – it only protects against OpenTofu runs with this configuration, not against docker volume rm, a deleted line of code or a broken disk.

▶
Break it yourself and find it – free
OpenTofu hands-on, Day 4: count, for_each and lifecycle – multiple instances and protection against deletion.
Go to day 4 →

FAQ

Why is nothing deleted at all?

OpenTofu checks the plan before executing it. If it violates prevent_destroy, the run is rejected completely.

Does prevent_destroy also protect when renaming?

Yes – if the plan deletes and recreates the resource, the protection applies too. Renames are handled with moved blocks (Day 11).

Related error patterns

More