How to Remove a Resource from Terraform State

Terraform state reflects the most up to date reference of the infrastructure.

Sometimes you may need to remove a resource from the Terraform state manually. This is helpful if a resource has been deleted or modified manually and the resource definition has been removed in your Terraform code. This way you can re-run your Terraform plan and apply without deleting the remote infra resource that you removed from the state.

Step 1 – Find the resource you want to delete

Running this command will place all resources managed by Terraform in a list form, all resources separated by line breaks.

terraform state list
Code language: Bash (bash)

Step 2 – Remove the resource in question

Once you know the resource you would like to remove from the state, pass it’s Terraform reference name to the rm command below:

terraform state rm <resource_name>
Code language: Bash (bash)

Example:

terraform state rm module.kms_key.aws_kms_key.default[0]
Code language: Bash (bash)
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments