Removing last record terraform state in a terraform workspace

Dear hashicorp community
I am using terraform cloud workspaces.
By mistake, I upload the wrong terraform state to my workspace. Now my terraform plan is using it, but I don’t want to use it since that is not the state I wanted to get.

Let me explain with this image: I want to use the state from 10 months ago and not the New State I got.

I want to go back to my old state since in the new one there are no some resources and then they are being recreated in my terraform plan.
I am trying to import every separate resource, by executing terraform import <RESOURCE-INSTANCE> <ID> command in this way:

terraform import azurerm_app_service_plan.fem-plan /subscriptions/d37e88d5-e443-4285-98c9-91bf40d514f9/resourceGroups/rhd-spec-prod-rhdhv-1mo5mz5r2o4f6/providers/Microsoft.Web/serverfarms/fem-plan-afd1
Acquiring state lock. This may take a few moments...
azurerm_app_service_plan.fem-plan: Importing from ID "/subscriptions/d37e88d5-e443-4285-98c9-91bf40d514f9/resourceGroups/rhd-spec-prod-rhdhv-1mo5mz5r2o4f6/providers/Microsoft.Web/serverfarms/fem-plan-afd1"...
azurerm_app_service_plan.fem-plan: Import prepared!
  Prepared azurerm_app_service_plan for import
azurerm_app_service_plan.fem-plan: Refreshing state... [id=/subscriptions/d37e88d5-e443-4285-98c9-91bf40d514f9/resourceGroups/rhd-spec-prod-rhdhv-1mo5mz5r2o4f6/providers/Microsoft.Web/serverfarms/fem-plan-afd1]


Error: Cannot import non-existent remote object

While attempting to import an existing object to
azurerm_app_service_plan.fem-plan, the provider detected that no object exists
with the given id. Only pre-existing objects can be imported; check that the
id is correct and that it is associated with the provider's configured region
or endpoint, or use "terraform apply" to create a new remote object for this
resource.

Releasing state lock. This may take a few moments...

But I got in the output, that resource does not exist, because terraform is using my latest New state where that resource is not included.

How can I use my old 10 months ago state?
I will appreciate your help

Unfortunately, there’s no particularly smooth way to roll back to an earlier version of state. However, you should be able to straighten this out by uploading the old state version a second time so that the workspace considers it to be the latest!

First you’ll need to find the state file you’d like to roll back to and download it to your local machine (you should see a “Download” button when looking at the state version in the UI).

Then you should be able to use the terraform state push command to re-upload that state file (you may need the -force option). In a pinch, you can use the API directly.

1 Like

Hi @radditude , indeed as you point me out, I use terraform state push to upload my desired state. This article helps as well Modifying Terraform states in Terraform Enterprise or Terraform Cloud – HashiCorp Help Center