Introduction
This guide outlines how to remove a resource from the Terraform state file using the removed block, which was introduced in Terraform v1.7. This feature is useful when you want to stop managing a resource with Terraform but do not wish to destroy the corresponding infrastructure.
When you delete a resource block from your configuration, Terraform's default behavior is to plan the destruction of the actual infrastructure resource. The removed block overrides this behavior, allowing you to remove a resource from Terraform’s state while ensuring the real infrastructure object remains intact. After applying this change, the resource is no longer tracked by Terraform, but it is not deleted from your cloud environment.
Prerequisites
- Terraform v1.7 or later.
- A Terraform workflow, such as local CLI execution or a VCS-driven workflow in HCP Terraform or Terraform Enterprise.
Procedure
Follow these steps to remove a resource from the Terraform state without destroying it.
1. Identify the Resource
First, identify the resource you want to remove from Terraform's management. Note the resource's full address as it appears in your configuration or state file (e.g., aws_instance.example).
2. Update the Terraform Configuration
In your configuration file, remove the original resource block and replace it with a removed block. This block instructs Terraform to stop managing the resource without destroying it.
For example, to remove a resource with the address null_resource.null, you would use the following configuration.
removed {
from = null_resource.null
lifecycle {
destroy = false
}
}-
from: Specifies the address of the resource you are removing from state. -
lifecycle: This block is required and controls Terraform's behavior. Thedestroy = falseargument ensures that the resource is removed from Terraform’s state but the actual infrastructure remains untouched.
3. Apply the Configuration
Run the standard Terraform workflow to apply the configuration change and update the state file.
-
Initialize your Terraform configuration.
$ terraform init
-
Create an execution plan to preview the changes.
$ terraform plan
The output shows that the resource will no longer be managed by Terraform but will not be destroyed.
Terraform will perform the following actions: # null_resource.null will no longer be managed by Terraform, but will not be destroyed # (destroy = false is set in the configuration). resource "null_resource" "null2331" { id = "1169332259171977768" } -
Apply the changes to update the state file.
$ terraform apply
4. Verify the Removal
After the apply completes, confirm that Terraform no longer tracks the resource.
-
CLI Verification: Run the
terraform state listcommand and confirm that the resource address is no longer in the output. - UI Verification: In HCP Terraform or Terraform Enterprise, navigate to your workspace's Resources tab in the UI and confirm the resource is no longer listed.
Additional Information
For more details on this feature, refer to the official Removing Resources Documentation.