Removing Existing Resources from State
Danger, Will Robinson
Removing resources from state will prevent you from managing the resources with Terraform. You may or may not be able to reimport the resource. Consider other options first, and proceed with caution.
Why
Removing a resource from state allows you to disassociate a resource from your Terraform state configuration without destroying the resource. This may be helpful under specific conditions, e.g.:
- Rectifying drift with respect to resources no longer actively managed by code
- Providing specific resources to a team that will manage by click ops
- Troubleshooting a corrupted resource
- Decommissioning a resource that was manually removed
There are several risks you should consider before removing a resource from state:
- You run the risk of corrupting the state file. As a precaution, maintain a versioned back up.
- The resource may get recreated by code if it still exists in your TFVARS file. If the resource still exists, it could get overwritten.
- If other resources reference the removed resource, your plan or apply may fail. Or, the dependent resources could get destroyed.
- There is no audit trail to this action.
After removing a resource from state, you may still reference it using a data block. Some, but not all, resources may also allow you to import them back to state.
How
Expect the unexpected
As a precautionary step, ensure that you either have a backup of the state file or have enabled storage versioning where the state file is kept. In the event of disaster, you may restore the state file from backup.
- (Optional) Obtain a list of all resources in state. You can use this to identify or confirm resources, but we won't specifically use it for the following steps, but you would use it to identify a resource that is no longer in code.
- Comment out the resource block that you wish to remove from state in your TFVARS.
- Run a terraform plan and output it to a text file. You will use this to identify the resource(s) you wish to remove from state.
-
Use Notepad++ to open the text file and perform a find and replace. Confirm Search Mode is set to Regular expression. Find the regex
^(?!.*destroyed).*$\nand replace with nothing.Info
This find and replace intends to isolate just the resource(s) you are destroying, i.e. the resource(s) you commented out in step (2).
Notepad++ Screenshot

-
All resources must be trimmed at the start and the end so they look like this, including the single quotes.
'module.disk.aws_ebs_volume.disks["odbppe1.PNCT-EPDODBPPE1.INST"]'
- For all resources you will be removing from state, put them on a single line, space-delimited. You can use Notepad++ Find/Replace to do this. Find:
\n, replace with<space>. - Now that you have your list of resources to remove from state, run a dry run and review the results.
- If expectations == reality and the expected resources will be removed, you can remove the
-dry-runflag to actually remove the resources. - Re-run the plan and confirm desired results.
Additional resources
-
The remove-from-state.yml pipeline can be used to remove resources from state. Credit to Andres Realpozo.
Additional information needed
This section is still in progress. Please exercise caution when using the remove-from-state pipeline.