How to Remove a Resource from a Terraform State File Using `terraform state rm`

The terraform state rm command permanently deletes resource instances from the Terraform state file by removing their addresses from the internal resource map, effectively untracking infrastructure without destroying actual cloud resources.

Managing infrastructure state is a critical aspect of Terraform workflows, and sometimes you need to disassociate a resource from Terraform's control without deleting the underlying infrastructure. The terraform state rm command provides a safe, CLI-driven way to remove specific resource addresses from your state file, whether you're cleaning up dangling resources or fixing state corruption. This operation is implemented in the hashicorp/terraform repository, specifically within internal/command/state_rm.go for command handling and internal/states/state.go for the underlying state manipulation logic.

What Is terraform state rm?

The terraform state rm command is a state management operation that directly edits the Terraform state by deleting one or more resource instances (addresses) from it. When executed, Terraform loads the current state—whether from a local terraform.tfstate file or a remote backend such as S3 or Terraform Cloud—removes the specified addresses, and writes the modified state back. This ensures the state file remains valid JSON and updates internal indexing structures to reflect the removal.

When to Remove Resources from State

There are several scenarios where removing resources from the Terraform state is necessary:

  • Cleaning up manually deleted resources – When infrastructure is deleted outside of Terraform (via console or CLI), the state becomes out of sync and requires cleanup.
  • Fixing state corruption – When a resource block no longer matches the configuration due to refactoring or manual edits.
  • Untracking mistaken resources – When resources were created by error and should never have been managed by Terraform.
  • Migrating resources – When moving resources to different Terraform workspaces or configurations.

How terraform state rm Works Internally

Understanding the implementation helps ensure safe usage of this destructive operation. The command follows a clear path through three key files in the hashicorp/terraform source code.

CLI Parsing in state_rm.go

The command definition and argument parsing occur in internal/command/state_rm.go. This file defines the command structure, validates supplied resource addresses, and handles flags such as -dry-run. The CLI layer ensures that only valid address syntax is passed to the core state manipulation functions.

State Modification in state.go

The actual removal logic resides in internal/states/state.go within the RemoveResource function. This routine walks the state's resource map, drops the matching entries, and recomputes the state's serial number to maintain consistency. The function ensures that after removal, the state file remains structurally valid and that no orphaned dependencies remain in the internal indexing structures.

Command Registration in commands.go

In commands.go, the "state rm" sub-command is registered as a first-class CLI operation, linking the user-facing command to the implementation in state_rm.go. This registration pattern allows Terraform to expose state management as a cohesive sub-command group alongside state list and state show.

Practical Usage Examples

The terraform state rm command supports several addressing patterns to handle different removal scenarios.

Remove a Single Resource

Delete a specific resource instance by its full address:

terraform state rm aws_instance.example

Remove Multiple Resources

Target multiple resources in a single command to reduce state file write operations:

terraform state rm aws_s3_bucket.bucket1 aws_s3_bucket.bucket2

Use Wildcard Patterns

Remove all instances of a particular resource type using wildcard syntax:

terraform state rm 'aws_security_group.*'

Remove from Specific Workspaces

When using Terraform workspaces, select the target workspace first, then remove the resource:

terraform workspace select dev
terraform state rm azurerm_resource_group.rg

Follow this sequence to safely remove resources from your Terraform state:

  1. Inspect current state – Run terraform state list to identify the exact resource addresses you intend to remove.
  2. Backup state – Create a backup of your state file before modification, especially when using local state.
  3. Execute removal – Run terraform state rm <address> with the specific resource address.
  4. Verify removal – Run terraform state list again to confirm the resource no longer appears in the state.
  5. Plan changes – Execute terraform plan to review how Terraform will treat the resource on the next apply—it will attempt to create it if the configuration still exists, or ignore it if the configuration was removed.

Summary

  • terraform state rm modifies the state file by removing specific resource addresses from the internal resource map defined in internal/states/state.go.
  • The command supports single resources, multiple addresses, and wildcard patterns for flexible resource targeting.
  • State removal is implemented in internal/command/state_rm.go and registered via commands.go in the hashicorp/terraform repository.
  • Always verify resource addresses with terraform state list before removal and back up your state file when working with local storage.
  • This operation does not destroy actual infrastructure—it only removes Terraform's tracking reference, making it safe for cleaning up manually deleted resources.

Frequently Asked Questions

Will terraform state rm delete my actual cloud resources?

No. The terraform state rm command only modifies the Terraform state file stored in internal/states/state.go by removing the resource's entry from the state map. Your actual infrastructure remains untouched in the cloud provider. Terraform will simply no longer track the resource, meaning the next terraform plan will show an intent to create a new resource unless you also remove the corresponding configuration block.

Can I undo terraform state rm if I remove the wrong resource?

Not directly through Terraform. Once executed, terraform state rm commits the change to the state file and increments the serial number in the state metadata. To recover, you must manually re-import the resource using terraform import with the correct resource address and provider ID, or restore from a state backup if you created one prior to the removal operation.

Why does Terraform update the serial number when removing a resource?

According to the implementation in internal/states/state.go, specifically within the RemoveResource routine, Terraform increments the state serial number to ensure that concurrent operations can detect state changes. This serial number acts as a versioning mechanism that prevents race conditions when multiple users or automation pipelines attempt to modify the same remote state simultaneously.

Does terraform state rm work with remote backends like S3 or Terraform Cloud?

Yes. The command functions identically regardless of state storage location. Terraform loads the current state from the configured backend (whether local terraform.tfstate or remote), applies the removal logic from internal/command/state_rm.go, and writes the modified state back to the same backend. The abstraction layer in the state management code ensures consistent behavior across all storage backends.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s https://instagit.com/install.md

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client