Skip to content
Latchkey

terraform refresh Command Reference

Reconcile state with the real world, the modern and safe way.

terraform refresh updates state to match real infrastructure. The standalone command is deprecated; the supported path is terraform apply -refresh-only, which lets you review drift before accepting it.

What it does

A refresh reads each managed resource from its provider and updates the recorded attributes in state without changing configuration, surfacing drift. The legacy terraform refresh applies those updates silently; terraform apply -refresh-only shows them and asks before writing.

Common flags and usage

  • -refresh-only: refresh mode for plan or apply (preferred over the standalone command)
  • -auto-approve: accept the state update without a prompt
  • -detailed-exitcode: with plan, exit 2 when drift is detected
  • -refresh=false: skip the implicit refresh when you have already refreshed

Example

shell
# Drift gate: exit 2 means drift was detected
terraform plan -refresh-only -input=false -detailed-exitcode

In CI

Replace deprecated terraform refresh with terraform apply -refresh-only -auto-approve, or gate on terraform plan -refresh-only -detailed-exitcode and alert instead of auto-accepting surprise drift.

Key takeaways

  • The standalone refresh is deprecated; use apply -refresh-only instead.
  • plan -refresh-only -detailed-exitcode returns 2 when drift exists.
  • Review drift before accepting it rather than silently overwriting state.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →