<p>It sounds like you have bigger problems.</p>
<p>From the documentation:</p>
<p><a href="https://developer.hashicorp.com/terraform/cli/commands/refresh" rel="nofollow noreferrer"><code>terraform refresh</code></a>:</p>
<blockquote>
<p>The terraform refresh command reads the current settings from all managed remote objects and updates the Terraform state to match.</p>
</blockquote>
<p>It goes on to say:</p>
<blockquote>
<p><strong>You shouldn't typically need to use this command</strong>, because Terraform automatically performs the same refreshing actions as a part of creating a plan in both the terraform plan and terraform apply commands. This command is here primarily for backward compatibility, but <strong>we don't recommend using it because it provides no opportunity to review the effects</strong> of the operation before updating the state.</p>
</blockquote>
<p>Meaning that the command is working as expected -- but not as you want it to.</p>
<p>If you have the &quot;proper&quot; (<em>i.e.</em> <strong>desired</strong>) state described in a Terraform statement (<em>e.g.</em> <code>main.tf</code>), then you should be able to converge back to the desired state by performing another <code>plan</code>/<code>apply</code> loop:</p>
<p><code>terraform plan</code> --&gt; should show the drift in state, <em>i.e.</em> there was a resource deleted and needs to be created.
<code>terraform apply</code> --&gt; the change will be applied and your state will converge to the declaration as specified in the Terraform file.</p>

CC BY-SA 4.0
2023-02-27T17:08:16.577Z
2023-02-27T17:08:16.577Z
Bruce Becker
<p>Posting the exact bit that worked for me. Terraform (in my case at least) compared what was demanded by the tf files to what was in the state and noted that the VM was missing. It properly re-created it, checking that the planned actions were correct. YMMV.</p>
<pre><code>terraform apply -state=project.tfstate -var-file=project.tfvars.json
</code></pre>

CC BY-SA 4.0
2023-03-01T07:33:49.367Z
yes, this is exactly the same as what I wrote in my answer. I'm not sure what mileage may vary, this is 100% predictable behaviour by terraform.
Bruce Becker
CC BY-SA 4.0
2023-03-13T16:00:19.553Z
Another guy on my team warned that dependencies can get complicated and other things could get wiped out as part of rebuilding that resource. While I wanted to post the precise command that worked for me, I didn't want people running it without thinking.
Elros
CC BY-SA 4.0
2023-02-28T22:35:07.350Z
2023-03-13T16:00:58.020Z
2023-03-13T16:00:58.020Z
Elros
<p>I have a terraform managed infrastructure with a set of 4 virtually identical VMs. One of them has been deleted in VSphere by mistake. I had expected <code>terraform refresh</code> to re-create it, but I misunderstood the purpose of the command. Now the VM doesn't show up even in the <code>tfstate</code> file. Is there a way to make terraform recreate it without having to re-deploy the entire infrastructure?</p>

<terraform>
Can terraform re-create a vm that was manually deleted
Elros
# Can terraform re-create a vm that was manually deleted

(Asked by: username_0 on 2023-02-27)

I have a terraform managed infrastructure with a set of 4 virtually identical VMs. One of them has been deleted in VSphere by mistake. I had expected `terraform refresh` to re-create it, but I misunderstood the purpose of the command. Now the VM doesn't show up even in the `tfstate` file. Is there a way to make terraform recreate it without having to re-deploy the entire infrastructure?

**Tags:** terraform


---

## 2 answers

### Answer by username_1 on 2023-02-27. Score: 2 | Accepted answer

It sounds like you have bigger problems.

From the documentation:

[`terraform refresh`](https://developer.hashicorp.com/terraform/cli/commands/refresh):

> The terraform refresh command reads the current settings from all managed remote objects and updates the Terraform state to match.

It goes on to say:

> **You shouldn't typically need to use this command**, because Terraform automatically performs the same refreshing actions as a part of creating a plan in both the terraform plan and terraform apply commands. This command is here primarily for backward compatibility, but **we don't recommend using it because it provides no opportunity to review the effects** of the operation before updating the state.

Meaning that the command is working as expected -- but not as you want it to.

If you have the "proper" (*i.e.* **desired**) state described in a Terraform statement (*e.g.* `main.tf`), then you should be able to converge back to the desired state by performing another `plan`/`apply` loop:

`terraform plan` --> should show the drift in state, *i.e.* there was a resource deleted and needs to be created.
`terraform apply` --> the change will be applied and your state will converge to the declaration as specified in the Terraform file.


---

### Answer by username_0 on 2023-02-28. Score: 0

Posting the exact bit that worked for me. Terraform (in my case at least) compared what was demanded by the tf files to what was in the state and noted that the VM was missing. It properly re-created it, checking that the planned actions were correct. YMMV.
terraform apply -state=project.tfstate -var-file=project.tfvars.json

> **Comments:**
> **username_1**: yes, this is exactly the same as what I wrote in my answer. I'm not sure what mileage may vary, this is 100% predictable behaviour by terraform.  
> **username_0**: Another guy on my team warned that dependencies can get complicated and other things could get wiped out as part of rebuilding that resource. While I wanted to post the precise command that worked for me, I didn't want people running it without thinking.  

---

