Skip to content

Destroy provisioners in removed blocks not working when associated block is removed #38315

@rmbolger

Description

@rmbolger

Terraform Version

Terraform v1.9.8
on windows_amd64

Terraform Configuration Files

locals {
  mydata = "some data"
}

resource "terraform_data" "my_resource" {
  input = local.mydata

  provisioner "local-exec" {
    command = "echo 'create' ${self.input}"
  }
  provisioner "local-exec" {
    when    = destroy
    command = "echo 'destroy' ${self.input}"
  }
}

Debug Output

https://gist.github.com/rmbolger/6f0e88f5507b24c618b8c1b9991c00c2

Expected Behavior

The destroy should have been accompanied by the destroy provisioner action such as:

terraform_data.my_resource: Destroying... [id=cbe8227f-c376-7d66-c570-f1de26bfd1ed]
terraform_data.my_resource: Provisioning with 'local-exec'...
terraform_data.my_resource (local-exec): Executing: ["cmd" "/C" "echo 'destroy' some data"]
terraform_data.my_resource (local-exec): 'destroy' some data
terraform_data.my_resource: Destruction complete after 0s

Actual Behavior

The destroy skipped the destroy provisioner and only had output such as:

terraform_data.my_resource: Destroying... [id=2f505733-2593-e3c7-b829-678d8b4f9c12]
terraform_data.my_resource: Destruction complete after 0s

Steps to Reproduce

  1. Start with the provided terraform config.
  2. terraform apply --auto-approve
  3. Comment or remove the "my_resource" block
  4. terraform apply --auto-approve

Additional Context

Prior to 1.9, one had to use workarounds like count=0 on a block during an Apply before actually removing the block in order to get the destroy-time provisioner to run. In the Terraform 1.9.0 release notes, one of the enhancements reads:

removed blocks can now declare destroy-time provisioners which will be executed when the associated resource instances are destroyed. (#35230)

I assumed this meant we would no longer be required to use the workarounds and could just remove the blocks and the cached state would still cause the last known value of the destroy provisioner to run. Is this broken or have I misunderstand what this enhancement was doing?

We are currently running on 1.9.8 in our environment. But I've tested this on 1.14.7 and the functionality has not changed.

I should also note that explicitly running apply with the -destroy switch does run the destroy provisioner. But I don't want to destroy everything in my solution, just the one block I removed.

References

https://github.com/hashicorp/terraform/blob/v1.9/CHANGELOG.md
#35230

Generative AI / LLM assisted development?

n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugnewnew issue not yet triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions