How can I remove a resource from Terraform state?

Sometimes, you may need to remove a resource from Terraform state without deleting the actual resource. This is useful when you want Terraform to stop managing a resource.
Why Remove a Resource from State?
There are several reasons you might want to remove a resource from Terraform state:
- Manual Management: You want to manage the resource manually outside of Terraform.
- Resource Migration: The resource is being moved to another Terraform configuration.
- State Cleanup: The resource is no longer relevant to your current configuration.
Steps to Remove a Resource from State
To safely remove a resource from Terraform state, follow these steps:
Step 1: Identify the Resource
Run the following command to list all resources in the state file:
terraform state list
This will display a list of all resources managed by Terraform. Note the name of the resource you want to remove.
Step 2: Remove the Resource
Use the terraform state rm command to remove the resource from the state file:
terraform state rm aws_instance.example
Step 3: Verify the State
Run terraform plan to ensure the resource is no longer managed by Terraform. The plan should not include any actions for the removed resource.
Best Practices
- Backup State Files: Always back up your state file before making changes.
- Document Changes: Keep a record of resources removed from the state for future reference.
- Use Remote Backends: Store your state file in a remote backend for better security and collaboration.
By following these steps, you can safely remove resources from Terraform state without impacting your infrastructure.
Try it hands-on
Run the commands from this article in the browser. Nothing to install.
We earn commissions when you shop through the links below.
Svix
Webhooks as a service
Svix Dispatch sends your webhooks for you: retries with exponential backoff, signed payloads, idempotency keys, and a delivery log your customers can see.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
Initial setup of Terraform backend using Terraform
Learn how to configure a Terraform backend for secure and efficient state management.
Terraform State Management
What is Terraform state, why is it important, and how do you manage state in a team environment?
mid
Terraform Repository Structure Checklist
Best practices for organizing and structuring your Terraform projects for maintainability and scalability.
30-45 minutes