terraform lifecycle_error ai_generated true

Error: Instance cannot be destroyed: resource has lifecycle.prevent_destroy set

ID: terraform/tf-lifecycle-prevent-destroy

Also available as: JSON · Markdown
90%Fix Rate
92%Confidence
70Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

Terraform plan includes destroying a resource that has prevent_destroy lifecycle rule enabled.

generic

Workarounds

  1. 92% success Remove or set prevent_destroy = false before destroying
    Update the lifecycle block: lifecycle { prevent_destroy = false }, then run terraform apply, then destroy

    Sources: https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle

  2. 85% success Use terraform state rm to remove from state without destroying the actual resource
    terraform state rm RESOURCE_ADDRESS removes the resource from Terraform management without destroying it in the cloud

    Sources: https://developer.hashicorp.com/terraform/cli/commands/state/rm

Dead Ends

Common approaches that don't work:

  1. Use terraform destroy -target to bypass prevent_destroy 90% fail

    The -target flag does not override lifecycle rules; prevent_destroy still blocks the destroy

  2. Modify the state file directly to remove the resource 75% fail

    Direct state modification is error-prone and the resource still exists in the cloud, creating drift

Error Chain

Leads to:
Preceded by:
Frequently confused with: