Removing Items from Terraform State
Learn how to use the rm subcommand in Terraform.
The rm
subcommand
The terraform state rm
command is used to remove items from the Terraform state.
Syntax
The rm
subcommand has the following syntax: terraform state rm [options] ADDRESS
.
Use of the terraform state rm
command
We can use this command to remove a single resource, a single instance of a resource, entire modules, and more. The resources removed by the command are removed from the state file but aren’t destroyed in the target environment.
The most common use for the rm
command is to remove a resource from Terraform management. The resource may be added to another configuration and imported, or perhaps Terraform is being replaced by something else. In either case, the goal is to remove the reference to the resource without deleting the actual resource.
How to apply the terraform state rm
command
Let’s say that we’re no longer going to manage our aws_s3_bucket
with Terraform. First, we can run the terraform state rm
command:
$> terraform state rm aws_s3_bucket.terraform_test_bucket_edu
Removed aws_s3_bucket.terraform_test_bucket_edu
Successfully removed 1 resource instance(s).
Next, we execute the following command to show the Terraform state list. This command lists all resources in the state file so we can choose which resource item we want to delete.
Get hands-on with 1400+ tech skills courses.