Introduction
This article details how to use the cloud
backend to remove resources within a workspace managed by Terraform Cloud or Terraform Enterprise.
Use Case
Removing specific resources is useful in the following scenarios:
- When Terraform performed a partial apply, leaving the state file in a condition where it does not reflect reality.
- When the resource was manually deleted from the cloud provider and the state file needs to be updated.
Procedure
The remote backend stores the Terraform state and may be used to run state operations in Terraform Cloud/Enterprise. terraform state
supports all sub-commands: (list, mv, pull, push, rm, show)
- Ensure that the backend is defined in the Terraform configuration.
terraform { cloud { hostname = "my-tfe-hostname" # Optional for TFC
organization = "my-org" workspaces { name = "my-workspace" } } }Note: For versions older than Terraform v1.1.0 and Terraform Enterprise v202201-1, you should use the
remote
block instead:terraform { backend "remote" { hostname = "my-tfe-hostname" # app.terraform.io for TFC organization = "my-org"
workspaces { name = "my-workspace" } } } - Run
terraform init
to initialize the backend. You may need to runterraform login
as well if your local environment is not authenticated with Terraform Cloud/Enterprise - Run
terraform state list
to list all resources managed by the state file.$ terraform state list null_resource.testing
- Run
terraform state rm RESOURCE.ADDRESS
to remove the resource.$ terraform state rm null_resource.testing Removed null_resource.testing Successfully removed 1 resource instance(s).
After the resource has been identified, targeted, and removed, check the TFC/TFE Workspace’s State tab to verify that a new state has been updated.