The terraform state commands
list, show, mv, rm, pull and push: the commands that read and change state without hand-editing it, and the cases where a config block is the safer tool.
The terraform state commands
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `state list` and `state show` read state. `state mv` renames an address and `state rm` makes Terraform forget one. `pull` and `push` move the whole file in and out of a backend.
- `state rm` never touches the real object. It only deletes the record, so the next plan wants to create the thing again unless the code is removed too.
- Every subcommand that modifies state writes a backup file and takes the state lock, so a mistake is recoverable and two people cannot run them at once.
- `state mv` only moves between the same resource type. For anything a team shares, a `moved` block in code does the same job and is reviewed, reproducible and works on every environment.
- `-dry-run` on `mv` and `rm` shows what would change. Run it first.
When you need to talk to the file
Most days nobody touches state directly, because plan and apply do it. Then three things happen in the same quarter. Someone renames aws_s3_bucket.assets to aws_s3_bucket.notes and the plan wants to destroy and re-create it. A resource has to leave Terraform’s care but stay alive. A teammate asks “is the bastion in state, and what is its ID?” For each, the answer is a terraform state subcommand, and the previous lessons explain why you should not open the JSON instead.
Reading
terraform state list prints every resource address. Give it an address or module path to filter, or -id= to find a resource by its real ID. terraform state show ADDRESS prints one object’s attributes. terraform state pull prints the whole file from the backend, which is the thing to pipe into jq. Current docs also list terraform state identities -json, for resources whose providers report an identity. All of these read only: they write no backup and change nothing.
Writing
I can’t run Terraform here, so I wrote a small Python stand-in that works on the state file from the first lesson of this group. It reads and writes the same JSON, writes a .backup, and raises the serial. The command names are the real ones. The messages are my script’s, not Terraform’s.
$ state list
aws_s3_bucket.notes_uploads
random_password.db
aws_subnet.private["a"]
aws_subnet.private["b"]
$ state mv aws_s3_bucket.notes_uploads aws_s3_bucket.uploads
Move "aws_s3_bucket.notes_uploads" to "aws_s3_bucket.uploads"
Successfully moved 1 object(s).
$ state rm 'random_password.db'
Removed random_password.db
Successfully removed 1 resource instance(s).
$ state list
aws_s3_bucket.uploads
aws_subnet.private["a"]
aws_subnet.private["b"]
serial now 9
state mv SOURCE DESTINATION moves the real object tracked at one address to another. The docs describe the usual reasons: you renamed a resource block, or moved it into a module, and you want Terraform to see the same object rather than a delete and a create. Source and destination must be the same kind of thing, and a resource can only move to an address with the same resource type.
state rm ADDRESS deletes the record. The docs are blunt about the consequence: the object keeps existing in the remote system, and the next plan will include a create for each “forgotten” instance, which may fail if names clash. It is how you hand a resource to another configuration or stop managing it. The code has to go too, or Terraform will build a second copy. There is also -dry-run, which lists what a command would match without doing it.
state push writes a local file over the backend’s state, and state pull is its read side. Both sit under disaster recovery in the docs, for when a bad write needs a previous version put back. The lineage and serial checks apply: Terraform refuses a push that looks like a different or older state unless forced.
What they guard and what they don’t
Every subcommand that modifies state writes a backup file. The docs say so, and say that backups are written to disk even when state is remote, with -backup to set where. The mutating commands also take the lock from the previous lesson, so two of them cannot run against one state. -lock=false turns that off, and the docs warn it is dangerous.
What they don’t give you is review. state mv edits one copy of state, once, from one person’s terminal. A teammate’s local checkout and your staging environment’s state never saw it. After you run mv in prod, the same rename in staging still plans a destroy.
Prefer a block when others share the state
Terraform now has declarative versions of the common edits, and each is a few lines in a pull request:
- Renaming or moving an address: a
movedblock, covered in the next lesson. Terraform 1.8 extended it to moves between resource types when the provider supports converting between them. - Forgetting a resource without destroying it: a
removedblock withlifecycle { destroy = false }, available since Terraform 1.7 and OpenTofu 1.7. - Adopting a resource: an
importblock, covered in importing existing infrastructure.
I would still use the command for a one-off repair on a state nobody else shares, or inside a recovery, with -dry-run first. OpenTofu also accepts aliases: state ls, state move and state remove.
About the video
The embed is ProgrammingKnowledge’s “Mastering Terraform State Management: Essential Commands Explained (push, pull, list, show, mv, rm)”, published October 2025, 11 minutes. Its description lists the subcommands covered, which matches this lesson. It is from a small channel, has no chapters, and as far as its description shows it does not mention moved, removed or import blocks, which is why this lesson adds them. AS Technologies’ older state videos, in deeper, split list, show, mv, pull, push and force-unlock across several short episodes from 2021. English was judged from the title, description and English auto-caption track.
Where this fits
Commands are the manual tool. The refactoring tool that belongs in code review is the moved block: moved blocks and safe refactoring.
Check yourself
What happens to the real S3 bucket when you run `terraform state rm aws_s3_bucket.logs`?
Nothing. It stays in AWS. Terraform just stops tracking it. If the resource block is still in your code, the next plan proposes to create a new bucket, which may fail on the name.
Why is `terraform state mv` risky on a shared project, compared with a `moved` block?
It changes one copy of state by hand, once, with no review. Teammates and other environments that never ran it still see the old address and plan a destroy and create. A `moved` block travels with the code and fixes every state the same way.
Where does the backup of the state go when `state mv` runs against a remote backend?
To a file on the machine running the command, a `.backup` file beside the working directory unless `-backup` says otherwise. Backups are written locally even for remote state.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.