Skip to content
Latchkey

terraform refresh / -refresh-only: Usage & CI Errors

Reconcile state with the real world - the modern, 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 any configuration. It surfaces drift (changes made outside Terraform). The legacy terraform refresh applies those updates silently; terraform apply -refresh-only shows them and asks before writing.

Common usage

Terminal
# Preferred: review drift, then accept the state update
terraform apply -refresh-only

# Non-interactive refresh in CI
terraform apply -refresh-only -auto-approve

# Detect drift as a gate without changing state
terraform plan -refresh-only -detailed-exitcode

Common error in CI: deprecation warning / unexpected drift

Pipelines using the old command get "The refresh command is deprecated" and may overwrite state with surprising drift. Fix: replace terraform refresh with terraform apply -refresh-only -auto-approve, or gate on terraform plan -refresh-only -detailed-exitcode (exit 2 = drift) and alert instead of auto-accepting. Avoid blanket auto-refresh in apply by setting -refresh=false when you have already refreshed in a prior step.

Key options

OptionPurpose
-refresh-onlyRefresh mode for plan/apply
-auto-approveAccept the state update without prompt
-detailed-exitcodeExit 2 when drift is detected (plan)

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →