# Terraform does not respect the order of operations for modify operations

**URL:** <https://discuss.hashicorp.com/t/terraform-does-not-respect-the-order-of-operations-for-modify-operations/44130>\
**Category:** Terraform\
**Created:** [September 9, 2022, 3:01pm UTC](https://discuss.hashicorp.com/t/terraform-does-not-respect-the-order-of-operations-for-modify-operations/44130 "2022-09-09T15:01:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vladimir-avinkin](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/vladimir-avinkin/32/30618_2.png) [@vladimir-avinkin](https://discuss.hashicorp.com/u/vladimir-avinkin)\
**Post date:** [September 9, 2022, 3:01pm UTC](https://discuss.hashicorp.com/t/terraform-does-not-respect-the-order-of-operations-for-modify-operations/44130/1 "2022-09-09T15:01:01Z")

</div>

I think this ignored ticket describes the problem very well [Terraform deletes/modifies resource and its dependency simultaneously · Issue #23169 · hashicorp/terraform · GitHub](https://github.com/hashicorp/terraform/issues/23169)

In short - when the resource is updated in place, dependencies are ignored (implicit or explicit dependencies) and so is the order of operations

So if you need to delete one resource and update one that depends on the resource you are deleting terraform will try to delete the resource first and then modify the other, which is wrong since it depends on the resource that is being deleted.

Are there any workarounds for that behavior? Are you interested in fixing this? I can create a new github issue with a fairly simple reproducible scenario if you want

---

<div class="post-metadata">

**Author:** ![jbardin](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jbardin/32/3325_2.png) [@jbardin](https://discuss.hashicorp.com/u/jbardin)\
**Post date:** [September 9, 2022, 3:54pm UTC](https://discuss.hashicorp.com/t/terraform-does-not-respect-the-order-of-operations-for-modify-operations/44130/2 "2022-09-09T15:54:06Z")

</div>

Hi @vladimir-avinkin,

The linked issue would only be relevant for very old versions of terraform, so I suspect that the issue is not related to what you are describing.

The correct order for a deletion and update of its dependent is the order you listed, because by default terraform uses a delete-then-create ordering. If you want the update to happen _before_ the deletion, then you need to specify `create_before_destroy` in one of the resource’s `lifecycle` blocks.

---

<div class="post-metadata">

**Author:** ![vladimir-avinkin](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/vladimir-avinkin/32/30618_2.png) [@vladimir-avinkin](https://discuss.hashicorp.com/u/vladimir-avinkin)\
**Post date:** [September 11, 2022, 5:15pm UTC](https://discuss.hashicorp.com/t/terraform-does-not-respect-the-order-of-operations-for-modify-operations/44130/3 "2022-09-11T17:15:10Z")

</div>

No, you did not understand me.

I created the issue which has a reproduction case and hopefully a better explanation of what i mean

> <https://github.com/hashicorp/terraform/issues/31769>
>
> \### Terraform Version
> 
> \`\`\`shell
> terraform version
> Terraform v1.2.9
> on linux\_am…d64
> + provider registry.terraform.io/hashicorp/hashicups v0.3.1
> + provider registry.terraform.io/hashicorp/null v3.1.1
> \`\`\`
> 
> 
> \### Terraform Configuration Files
> 
> https://github.com/vladimir-avinkin/terraform-dependency-order-repro
> 
> \### Debug Output
> 
> https://gist.github.com/vladimir-avinkin/f948eb7d4917e2c590d4d3172f70e144
> 
> \### Expected Behavior
> 
> Terraform should respect the order of operations for dependent resources, in this case, null\_resource.null\[1\] should have been destroyed AFTER modifying hashicups\_order.cup, not before
> 
> \### Actual Behavior
> 
> Despite the explicit and explicit dependency terraform destroyed null\_resource.null\[1\] before modifying hashicups\_order.cup
> 
> \### Steps to Reproduce
> 
> Clone the repository, ensure that you have the required tools that are listed in README.md, then run:
> \`\`\`bash
> docker-compose up -d
> # make sure you wait for the service to be up before the next command
> curl -X POST localhost:19090/signup -d '{"username":"education", "password":"test123"}'
> terraform init
> # after that command terraform should successfully create three resources 
> terraform apply
> # now run apply with fewer dependencies
> terraform apply -var 'resource\_count=1'
> \`\`\`
>   
> In that last apply you will see that terraform deletes \`null\_resource.null\[1\]\` before modifying \`hashicups\_order.cups\` despite
> \`hashicups\_order.cups\` implicit and explicit dependencies
> 
> \### Additional Context
> 
> \_No response\_
> 
> \### References
> 
> \- I believe the same bug is described in that issue #23169
