OpenTofu dynamic `prevent_destroy` lifecycle setting
Introduced 2026-05-14 · score 0.99
HOW TO USE IT
When a managed resource’s destroy protection should follow a module value, set `lifecycle.prevent_destroy` to a same-module expression such as `var.prevent_destroy_database` (available in OpenTofu v1.12.0).
variable "prevent_destroy_database" {
type = bool
default = true
}
resource "example_database" "example" {
# ...
lifecycle {
prevent_destroy = var.prevent_destroy_database
}
}WHY THIS HELPS
Configure a managed resource so its destroy protection follows a module value.
REQUIREMENTS AND LIMITATIONS
- Version: v1.12.0
- lifecycle.prevent_destroy: Boolean lifecycle argument; when true, OpenTofu errors on plans that destroy the associated object. In v1.12.0 it can be set dynamically from same-module values.
- prevent_destroy_database: Example input variable of type `bool`; its default is true, making it optional to callers. Child-module callers pass its value in the module block.
- Prerequisite: Use OpenTofu v1.12.0 or later for dynamically defined `prevent_destroy`.
- Prerequisite: Declare any referenced input variable in the same module; the example uses `type = bool`.
- The referenced values must be available elsewhere in the same module.
FIRST-PARTY EVIDENCE
- https://github.com/opentofu/opentofu/releases/tag/v1.12.0
- https://opentofu.org/docs/v1.12/language/resources/behavior/
- https://opentofu.org/docs/v1.12/language/values/variables/
Check the installed version and the source before using it. Entry JSON