Bird
Raised Fist0
Terraformcloud~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the terraform state rm command do?
easy
A. Deletes the resource from the cloud provider and Terraform state
B. Removes a resource from Terraform's state without deleting the actual resource
C. Updates the resource configuration in Terraform files
D. Creates a new resource and adds it to the state

Solution

  1. Step 1: Understand the purpose of terraform state rm

    This command tells Terraform to stop tracking a resource by removing it from the state file.
  2. Step 2: Recognize it does not delete the actual resource

    The real resource remains intact in the cloud; only Terraform forgets it.
  3. Final Answer:

    Removes a resource from Terraform's state without deleting the actual resource -> Option B
  4. Quick Check:

    State removal only forgets resource [OK]
Hint: Remember: state rm forgets resource, does not delete it [OK]
Common Mistakes:
  • Thinking it deletes the actual resource
  • Confusing it with terraform destroy
  • Assuming it updates resource configuration
2. Which of the following is the correct syntax to remove a resource named aws_instance.example from Terraform state?
easy
A. terraform rm state aws_instance.example
B. terraform remove aws_instance.example
C. terraform state rm aws_instance.example
D. terraform state delete aws_instance.example

Solution

  1. Step 1: Recall the correct command structure

    The command to remove a resource from state is terraform state rm <resource_name>.
  2. Step 2: Match the resource name format

    The resource name is aws_instance.example, so the full command is terraform state rm aws_instance.example.
  3. Final Answer:

    terraform state rm aws_instance.example -> Option C
  4. Quick Check:

    Correct command syntax [OK]
Hint: Use 'terraform state rm' followed by resource name [OK]
Common Mistakes:
  • Swapping 'rm' and 'state' keywords
  • Using 'remove' or 'delete' instead of 'rm'
  • Incorrect command order
3. Given the following Terraform state command:
terraform state rm aws_s3_bucket.my_bucket

What will happen after running this command?
medium
A. Terraform will remove the S3 bucket from state but the bucket remains in AWS
B. Terraform will update the S3 bucket configuration in the state
C. Terraform will delete the S3 bucket from AWS and remove it from state
D. Terraform will show an error because the command is incomplete

Solution

  1. Step 1: Understand the effect of terraform state rm

    This command removes the resource from Terraform's state file only.
  2. Step 2: Recognize the real resource remains untouched

    The actual S3 bucket in AWS is not deleted or changed by this command.
  3. Final Answer:

    Terraform will remove the S3 bucket from state but the bucket remains in AWS -> Option A
  4. Quick Check:

    State removal keeps resource intact [OK]
Hint: State rm forgets resource, does not delete it in cloud [OK]
Common Mistakes:
  • Assuming the resource is deleted from AWS
  • Thinking the state file is updated with new config
  • Believing the command is incomplete
4. You ran terraform state rm aws_instance.web but later realized you still want Terraform to manage this instance. What is the best way to fix this?
medium
A. Run terraform import aws_instance.web <instance_id> to re-add it to state
B. Run terraform state add aws_instance.web to add it back
C. Run terraform state rm aws_instance.web again to undo
D. Delete the instance manually and recreate it

Solution

  1. Step 1: Understand that terraform state rm only removes from state

    Once removed, Terraform no longer tracks the resource.
  2. Step 2: Use terraform import to re-add the existing resource to state

    This command links the real resource back to Terraform's state file.
  3. Final Answer:

    Run terraform import aws_instance.web <instance_id> to re-add it to state -> Option A
  4. Quick Check:

    Import command restores resource to state [OK]
Hint: Use terraform import to re-add removed resources [OK]
Common Mistakes:
  • Trying to use a non-existent 'state add' command
  • Running state rm again expecting undo
  • Deleting resource unnecessarily
5. You want to stop managing a resource google_compute_instance.vm1 with Terraform but keep it running in your cloud. Which sequence of commands achieves this safely?
hard
A. Run terraform state rm google_compute_instance.vm1 then manually delete the resource
B. Run terraform destroy -target=google_compute_instance.vm1 to remove it from cloud and state
C. Remove the resource block from Terraform files and run terraform apply
D. Run terraform state rm google_compute_instance.vm1 only to remove it from state and keep it running

Solution

  1. Step 1: Use terraform state rm to remove resource from state

    This stops Terraform from managing the resource but does not delete it.
  2. Step 2: Avoid deleting the resource or removing config to keep it running

    Deleting or removing config and applying would delete or recreate the resource.
  3. Final Answer:

    Run terraform state rm google_compute_instance.vm1 only to remove it from state and keep it running -> Option D
  4. Quick Check:

    State rm forgets resource, keeps it running [OK]
Hint: State rm removes from Terraform control, resource stays alive [OK]
Common Mistakes:
  • Deleting resource after state rm
  • Using terraform destroy which deletes resource
  • Removing config without state rm causes resource deletion