0
0
Terraformcloud~10 mins

Terraform state rm for removing resources - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Terraform state rm for removing resources
Identify resource to remove
Run terraform state rm command
Terraform updates state file
Resource removed from state
Resource still exists in cloud
Next terraform apply ignores removed resource
This flow shows how Terraform removes a resource from its state file without deleting it from the cloud.
Execution Sample
Terraform
terraform state rm aws_instance.example
terraform plan
Removes the aws_instance.example resource from Terraform state, then shows plan ignoring it.
Process Table
StepCommandActionState ChangeCloud Resource
1terraform state rm aws_instance.exampleRemove resource from state fileaws_instance.example removedResource still exists
2terraform planPlan after removalNo reference to aws_instance.exampleResource still exists, not managed
3terraform applyApply changesNo changes to aws_instance.exampleResource unchanged in cloud
4terraform state listList resources in stateaws_instance.example not listedResource still exists
5terraform state rm aws_instance.exampleTry removing againError: resource not foundNo change
6ExitNo more actionsState stable without resourceCloud resource unmanaged
💡 Resource removed from state, so Terraform no longer manages it but resource remains in cloud.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
State fileContains aws_instance.exampleRemoved aws_instance.exampleNo reference to aws_instance.exampleNo reference to aws_instance.exampleNo reference to aws_instance.example
Cloud resourceExistsExistsExistsExistsExists
Key Moments - 3 Insights
Why does the resource still exist in the cloud after running terraform state rm?
Because terraform state rm only removes the resource from Terraform's state file, it does not delete the actual resource in the cloud. See execution_table step 1 and 3.
What happens if you run terraform state rm on a resource not in the state?
Terraform shows an error saying the resource is not found in the state, as shown in execution_table step 5.
After removing a resource from state, will terraform plan try to change or delete it?
No, terraform plan will ignore the resource since it is no longer tracked in the state, as shown in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of aws_instance.example after step 1?
AIt is deleted from the cloud
BIt is unchanged in the state file
CIt is removed from the state file
DIt is renamed in the state file
💡 Hint
Check the 'State Change' column in execution_table row for step 1
At which step does terraform plan show no reference to aws_instance.example?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'State Change' columns for step 2 in execution_table
If you run terraform state rm on a resource not in the state, what happens?
ATerraform shows an error
BTerraform removes it anyway
CTerraform deletes the cloud resource
DTerraform adds the resource to the state
💡 Hint
See execution_table step 5 for the result of removing a non-existent resource
Concept Snapshot
terraform state rm <resource>
- Removes resource from Terraform state file only
- Does NOT delete resource in cloud
- Terraform stops managing the resource
- terraform plan/apply ignores removed resource
- Useful to detach resources without deleting
Full Transcript
Terraform state rm removes a resource from the Terraform state file but leaves the actual cloud resource untouched. This means Terraform no longer manages that resource. After running terraform state rm, terraform plan and apply will ignore the removed resource. If you try to remove a resource not in the state, Terraform will show an error. This command is useful when you want to stop managing a resource without deleting it from the cloud.