What is the difference between terraform refresh and terraform plan?
See how terraform refresh updates state to match reality while terraform plan previews changes, plus why refresh is deprecated for apply -refresh-only.
Expected Interview Answer
terraform refresh reconciles Terraform's state file with the real infrastructure by reading current attributes from providers, while terraform plan computes the difference between your configuration and state to show what changes would be applied.
Refresh only updates the recorded state to match reality — it does not look at your configuration or propose changes. Plan performs an implicit refresh (unless disabled with -refresh=false), then compares the refreshed state against the desired configuration to produce an add/change/destroy diff. Since Terraform 0.15+, standalone terraform refresh is deprecated in favor of terraform apply -refresh-only, because a silent state rewrite can hide drift that a plan would surface for review.
- Refresh keeps state accurate against real infrastructure
- Plan previews changes safely before applying
- Plan detects configuration drift explicitly
- Separating the two clarifies intent versus reality
- refresh-only apply lets you approve state updates instead of applying them silently
AI Mentor Explanation
terraform refresh is the scorer walking to the pitch to update the scorecard with the actual runs, wickets and overs that really happened on the ground. terraform plan is the coach comparing that up-to-date scorecard against the match plan and saying which bowlers to change to reach the target — one records reality, the other proposes the next moves.
Step-by-Step Explanation
Step 1
Understand state
Terraform stores the last-known attributes of each managed resource in a state file.
Step 2
Refresh reads reality
terraform refresh queries each provider for the resource's real attributes and writes them back into state.
Step 3
Plan refreshes first
By default plan runs an in-memory refresh so its diff is computed against current reality.
Step 4
Plan computes the diff
It compares the refreshed state to your configuration and reports resources to add, change or destroy.
Step 5
Prefer refresh-only apply
Use terraform apply -refresh-only to review and approve state updates instead of the deprecated standalone refresh.
What Interviewer Expects
- Clear distinction between updating state and proposing changes
- Awareness that plan performs an implicit refresh
- Knowledge of the -refresh=false flag and why it exists
- Understanding of configuration drift and how it surfaces
- Knowing terraform refresh is deprecated in favor of apply -refresh-only
Common Mistakes
- Claiming refresh changes infrastructure — it only updates state
- Thinking plan never touches state, ignoring its implicit refresh
- Believing refresh reads your configuration
- Not knowing standalone refresh is deprecated
- Confusing drift detection with actually applying changes
Best Answer (HR Friendly)
“Refresh just updates Terraform's records so they match what really exists in the cloud, without changing anything. Plan uses those up-to-date records to show you exactly what it would create, change or delete before you approve it.”
Code Example
# Deprecated: silently rewrites state to match reality
terraform refresh
# Preferred: review and approve state updates from drift
terraform apply -refresh-only
# Preview changes; runs an implicit refresh first
terraform plan
# Preview using existing state without refreshing (faster, may miss drift)
terraform plan -refresh=falseFollow-up Questions
- Why was standalone terraform refresh deprecated?
- What does the -refresh=false flag do and when would you use it?
- How does Terraform detect configuration drift?
- What is the risk of refreshing state automatically without review?
- How does a saved plan file interact with refresh?
MCQ Practice
1. What does terraform refresh primarily do?
Refresh reconciles the state file with the actual attributes of resources read from providers; it does not apply changes.
2. By default, terraform plan will:
Unless -refresh=false is passed, plan refreshes state in memory so its diff reflects current reality.
3. Which command is now recommended over standalone terraform refresh?
apply -refresh-only lets you review and approve state updates from drift instead of rewriting state silently.
Flash Cards
What does refresh change? — Only the state file, updating it to match the real infrastructure's current attributes.
Does plan touch state? — It performs an in-memory refresh by default but does not persist changes to infrastructure.
What replaced standalone refresh? — terraform apply -refresh-only, which lets you review state updates before saving them.
What is drift? — A difference between real infrastructure and recorded state, surfaced when Terraform refreshes and plans.