Is it possible to differentiate between a zero value and a removed property in the Terraform Provider SDK?

I’m working on a custom provider and I have one specific resource which can accept several optional (and mutually exclusive) integer properties. However, I’ve noticed that when I set a property to zero, then remove the property, the SDK does not detect this as a change. In other words, I cannot differentiate between an explicit zero value and the removal of this property.

I’ve seen these other related threads:

I’m guessing that I’m seeing another symptom of the same underlying issue. Is there a workaround here? Zero is a valid value for my integer field fwiw.

Hi @matthewcummings :wave: Thanks for asking about this and sorry can be confusing, at least when developing a provider with terraform-plugin-sdk.

One of the shortcomings with GitHub - hashicorp/terraform-plugin-sdk: Terraform Plugin SDK enables building plugins (providers) to manage any service providers or custom in-house solutions was that it was originally designed and implemented when Terraform did not implement the concept of null values. Under the hood, everything worked with Go’s zero-values for types when unset (e.g. “” for schema.TypeString, 0 for schema.TypeInt, etc.). When Terraform 0.12 introduced null values, the SDK was not redesigned around this support due to the amount of potential effort.

There may be certain provider code workarounds that can help in this situation, albeit sort of confusing to implement, such as using a Default value that is outside the expected values then ignoring that value in the actual create, read, update, and delete logic.

This null value limitation was one of the considerations when GitHub - hashicorp/terraform-plugin-framework: A next-generation framework for building Terraform providers. was being designed. Provider code using that can distinguish values between being null, unknown, or a known value of the type. For more information about the differences between terraform-plugin-sdk and terraform-plugin-framework, you can refer to Home - Plugin Development: Which SDK Should I Use? | Terraform by HashiCorp. The first version of the framework with code compatibility promises is being finalized over the next few months, at which it would be expected to be the recommended way to develop providers going forward.

Hope this helps at least a little.

Thank you for the response @bflad. I ended up switching to a string property(ies) and I’m using an empty string value to indicate that the property is unset or deleted.
I’m looking forward to the new version of the framework.