How to Remove a Resource from Terraform State

0 min read 143 words

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

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>

Example:

terraform state rm module.kms_key.aws_kms_key.default[0]
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags