Terraform
Remove a resource from state
To remove a resource from Terraform state without destroying it, replace the resource block with a removed block and then apply the change using the standard Terraform workflow. When you remove a resource from state, Terraform no longer manages that infrastructure's lifecycle. For information about how to remove a resource from state and destroy the actual infrastructure, refer to Destroy a resource.
Replace the
resourceblock from your configuration with aremovedblock.Configure the following arguments in the
removedblock:- Specify the address of the resource you want to remove in the
fromargument. You cannot include instance keys, such as"aws_instance.example[1]", if the resource is configured to provision multiple instances. - Add a
lifecycleblock to theremovedblock and set thedestroyargument tofalse. Settingdestroytotrueremoves the resource from state and destroys it. Refer to Destroy a resource for more information.
- Specify the address of the resource you want to remove in the
Delete any references to the resource's attributes. You can run the
terraform validatecommand to find references in your configuration.Run
terraform applyand confirm that you want to proceed when prompted.
The following example removes the aws_instance.example resource from state but does not destroy it:
removed {
from = aws_instance.example
lifecycle {
destroy = false
}
}
Alternatively, you can use the terraform state rm command to remove a resource from state, but we recommend using the removed block instead. This is because the removed block lets you preview the results of the operation, which makes it a safer way to remove resources.
If you want to resume managing the resource with Terraform, you must import the resource back into your configuration. Refer to Import existing resources for more information.