October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk6 min

Terraform State: Remove, Move, and Migrate Resources or Set Up a Remote Backend

Understand the difference between forgetting a resource, changing its address, transferring it between state files, and moving state to a remote backend.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Terraform state operations do different jobs: state rm forgets an object without destroying it, a move changes its Terraform address, a cross-state migration changes which state file manages it, and a backend migration changes where state is stored. Choose based on whether the infrastructure should remain, whether the state file changes, and whether the configuration should record the transition.

Choose the operation that matches your goal

Goal Recommended approach Does it destroy infrastructure? Does the resource stay in the same state file? Version and main caution
Stop managing an object but leave it running removed block with lifecycle { destroy = false } No Yes; its management binding is removed from that state Terraform 1.7 or newer; remove configuration references and review the plan
Immediately forget an object in state terraform state rm ADDRESS No Yes Preview matching addresses; a later plan may propose creating a replacement
Rename or relocate a resource address moved block or another configuration refactoring feature No, when Terraform recognizes the move Yes Keep the move history clear, especially for module users
Directly change an address in state terraform state mv SOURCE DESTINATION No, when used as intended Yes Source and destination must be compatible objects; coordinate the change
Transfer a resource to another state file removed and import blocks No, if the migration is configured correctly No Terraform 1.7 or newer for this workflow; confirm exclusive ownership and inspect both plans
Move state storage to another backend Update backend configuration, then run terraform init -migrate-state No Yes; the state is copied to different storage Back up first and verify workspace prompts and mappings

Remove Terraform management without destroying the object

If the infrastructure should continue to exist but Terraform should stop managing it, use a removed block with lifecycle { destroy = false } when possible. Terraform 1.7 introduced removed blocks. The configuration change can be reviewed through the usual plan and apply workflow; remove any configuration references to the resource’s attributes as needed. HashiCorp describes this reviewable approach as safer than directly editing state (resource removal documentation).

As an Amazon Associate I earn from qualifying purchases.

removed {
  from = aws_instance.example

  lifecycle {
    destroy = false
  }
}

Use terraform state rm ADDRESS when an immediate state edit is appropriate. It removes matching instance addresses from Terraform’s state; it does not delete the remote objects. Use -dry-run to see which addresses match before making the change, and leave state locking enabled unless there is a specific, understood reason to disable it (state rm command reference).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

After Terraform forgets an object, a later plan may propose creating it again if the configuration still declares it. That can fail if the proposed replacement conflicts with the existing object’s name or identifier. Removing management is not the same as destroying the infrastructure; if destruction is intended, use the normal Terraform resource-destruction workflow instead.

Rename or relocate an address within one state

A resource address changes when, for example, a resource block is renamed, moved into or out of a child module, or its instance structure changes. Without a declaration that connects the old and new addresses, Terraform can interpret the configuration change as one resource disappearing and another appearing, and propose deletion and creation.

Prefer a configuration-recorded move

For a refactor, use a moved block where supported. It records the relationship in configuration so Terraform can associate the existing object with its new address during planning. This is generally preferable for a change that should remain understandable to collaborators and users of a module (refactoring modules documentation).

Use state mv for a direct state edit

terraform state mv SOURCE DESTINATION directly changes an address in state. The source and destination must be the same kind of object, and a resource can move only to another address of the same resource type. Quote addresses containing bracketed count indices or for_each keys as required by your shell. Coordinate the configuration change and state operation so another run does not treat the transition as a destroy-and-create change (state mv command reference).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Transfer a resource between state files

A cross-state migration changes which state file is responsible for an object. First decide whether the object can reasonably be recreated: HashiCorp recommends recreating stateless resources when downtime and cost allow, while stateful databases and object stores may require a controlled transfer because deletion and recreation, or backup and restore, can be difficult (state move tutorial).

Rank #3

Use removed and import blocks for a new migration

For a new migration, HashiCorp recommends recording the handoff in configuration with removed and import blocks. These blocks are available for this workflow from Terraform 1.7 onward. Configure the source state to relinquish management without destroying the object, and the destination configuration to import it. Plan each side and confirm the object is managed by only one state before applying the changes.

Understand the legacy state mv route

Directly moving an object between state files with terraform state mv is a legacy alternative; the command supports this from Terraform 1.0 onward. For remote source and destination workspaces, HashiCorp’s documented procedure is to pull each state to a local file, move the resource between files with -state and -state-out, then push both updated files back. Back up both states and coordinate a freeze so concurrent Terraform runs cannot manage the object during the handoff. HashiCorp warns that manually updating remote state carries corruption risk and recommends the newer block workflow for new migrations (state move tutorial).

State commands are advanced operations. Treat direct state editing as a last resort, preserve backups, and check the resulting plans before proceeding (state commands overview).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Move state to a remote backend

A backend migration changes the storage location for state; it does not, by itself, transfer resource ownership to a different state file. Backend configuration belongs in Terraform configuration. After changing that configuration, initialize Terraform again before planning, applying, or running state commands.

  1. Back up the current state. HashiCorp says: “Before migrating to a new backend, we strongly recommend manually backing up your state by copying your terraform.tfstate file to another location.” (backend configuration documentation)
  2. Update the backend configuration in the Terraform configuration to specify the destination backend, following that backend’s current documentation.
  3. Run terraform init -migrate-state. Terraform attempts to copy existing state to the new backend and may ask you to confirm migration of workspace states.
  4. Review workspace prompts and mappings. Confirm that each source workspace will land in the intended destination before accepting the migration.
  5. Inspect the resulting state and plan before making follow-on infrastructure changes.

-force-copy automatically enables migration and answers yes to migration prompts. Use it only when you intend to skip interactive confirmation and have already checked the destination and workspace mapping. Do not use -reconfigure as a migration flag: it disregards the existing backend configuration and prevents state migration (terraform init command reference).

Protect credentials, locking, and collaboration

  • .terraform/ stores the most recent backend configuration, including authentication parameters supplied to the CLI. Do not commit this directory because it may contain sensitive credentials (backend configuration documentation).
  • Preserve state locking where the backend supports it, and coordinate migration timing with collaborators so concurrent runs do not write state during the transition.
  • Terraform state CLI commands can work with remote state, but each read and write requires a network round trip. Modifying commands also write backup files, which cannot be disabled (state command documentation).
  • State is sensitive operational data. Store backups securely and verify that access controls for the destination backend are appropriate.

Migrate existing state to HCP Terraform

For existing local or state-backend data, the HCP Terraform CLI integration prompts during terraform init to migrate state to HCP workspaces and may also prompt to rename workspaces. Terraform CLI workspaces can represent environments that share configuration; HCP workspaces represent independent configurations and require unique names within an organization. If the directory already uses the HCP remote backend, HashiCorp documents replacing that backend block with a cloud block to continue using the same HCP workspaces (HCP Terraform migration documentation).

Do not treat tf-migrate as a universal migration tool: HashiCorp marks it deprecated and unsupported. Its documented source-backend coverage excludes existing cloud integration and remote backend sources (tf-migrate documentation).

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.