Resources removed from tfstate after upgrade of my provider

Hello,

I am writing my own terraform provider for netbox: https://github.com/smutel/terraform-provider-netbox.

I created some resources with the version 0.1.0.
Now I released a new version 0.2.1.

For now I didn’t change any tf code. When I execute a plan with the same code 4 resources need to be created instead of zero.

I have also there errors in the log:

2020/06/29 21:39:45 [DEBUG] Resource instance state not found for node "module.tenant_infra-test_netbox_tenant.netbox_tenancy_tenant.tenant", instance module.tenant_infra-test_netbox_tenant.netbox_tenancy_tenant.tenant
2020/06/29 21:39:45 [DEBUG] Resource instance state not found for node "module.tenant_infra_netbox_tenant.netbox_tenancy_tenant.tenant", instance module.tenant_infra_netbox_tenant.netbox_tenancy_tenant.tenant
2020/06/29 21:39:45 [DEBUG] Resource instance state not found for node "module.tenant_infra-test_netbox_tenant.netbox_ipam_prefix.prefix", instance module.tenant_infra-test_netbox_tenant.netbox_ipam_prefix.prefix
2020/06/29 21:39:45 [DEBUG] Resource instance state not found for node "module.tenant_infra_netbox_tenant.netbox_ipam_prefix.prefix", instance module.tenant_infra_netbox_tenant.netbox_ipam_prefix.prefix

What I did wrong in my provider ?
Is-it related to the consul backend I am using ?

Thanks.

Either something happened to your state file and you lost those four resources, or something happened in the provider to remove them from your state file silently. Without more information, it’s hard to tell.

I found the issue. For some reason due to the targeted API, the exists function and read function of my provider didn’t found anymore the resources present in the tfstate.

So I assume that terraform remove items in the tfstate if the provider does not found them, right ?

The exists function will automatically. The read function will only if you call ResourceData.SetId("").