{"id":47475,"date":"2024-11-26T16:10:24","date_gmt":"2024-11-26T16:10:24","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=47475"},"modified":"2024-11-26T16:10:24","modified_gmt":"2024-11-26T16:10:24","slug":"terraform-tutorials-how-to-remove-a-resource-from-your-terraform-configuration-state-file","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/terraform-tutorials-how-to-remove-a-resource-from-your-terraform-configuration-state-file\/","title":{"rendered":"Terraform Tutorials: How to remove a resource from your Terraform configuration &amp; state file"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Step-by-Step Tutorial for Using the <code>removed<\/code> Block in Terraform<\/p>\n<\/blockquote>\n<\/blockquote>\n<\/blockquote>\n\n\n\n<p>Step by Step in terraform To remove a resource from your Terraform configuration &amp; state file without destroying the real infrastructure object it manages<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Old Method &#8211; Using Command Line<\/h2>\n\n\n\n<p>Here is a step-by-step guide to remove a resource from your Terraform configuration and state file without destroying the real infrastructure object:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Identify the Resource to Be Removed<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Locate the resource in your Terraform configuration that you want to remove.<\/li>\n\n\n\n<li>Note the resource&#8217;s name and type (e.g., <code>aws_instance.example<\/code>).<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Run the <code>terraform state rm<\/code> Command<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>terraform state rm<\/code> command removes a resource from the Terraform state file without affecting the real infrastructure.<\/li>\n\n\n\n<li>Use the following syntax: <code>terraform state rm &lt;resource_address><\/code><\/li>\n\n\n\n<li>Example: <code>terraform state rm aws_instance.example<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Verify State File Update<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Check the Terraform state to ensure the resource has been removed: <code>terraform state list<\/code><\/li>\n\n\n\n<li>Confirm that the resource no longer appears in the list.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Remove the Resource from Configuration<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open your Terraform configuration file (<code>*.tf<\/code>).<\/li>\n\n\n\n<li>Delete the corresponding block of the resource you removed from the state file.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Run <code>terraform plan<\/code> to Validate<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Execute a <code>terraform plan<\/code> to ensure the configuration aligns with the state: <code>terraform plan<\/code><\/li>\n\n\n\n<li>Verify that Terraform does not attempt to recreate the resource you just removed.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Apply Changes (If Necessary)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the <code>terraform plan<\/code> shows changes due to dependent resources, apply those changes to update the infrastructure: <code>terraform apply<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Best Practices<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Backup the State File:<\/strong> Always backup your <code>terraform.tfstate<\/code> file before making changes. <code>cp terraform.tfstate terraform.tfstate.bak<\/code><\/li>\n\n\n\n<li><strong>Use Workspaces if Required:<\/strong> Ensure you are in the correct workspace if you&#8217;re managing multiple environments.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Result<\/strong><\/h3>\n\n\n\n<p>The resource is no longer managed by Terraform but still exists in the real infrastructure.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">New Method using Removed Block<\/h2>\n\n\n\n<p>The <code>removed<\/code> block in Terraform (introduced in version <strong>1.7<\/strong>) provides a safe way to remove resources from the Terraform state without destroying them in the real infrastructure. This is especially useful when you want Terraform to stop managing specific resources but retain their existence in your cloud environment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is the <code>removed<\/code> Block?<\/strong><\/h3>\n\n\n\n<p>The <code>removed<\/code> block explicitly informs Terraform that certain resources are no longer managed by the configuration. During <code>terraform apply<\/code>, Terraform removes these resources from the state without attempting to delete them in the actual infrastructure.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Step 1: Identify the resource<\/span>\nresource <span class=\"hljs-string\">\"aws_instance\"<\/span> <span class=\"hljs-string\">\"example\"<\/span> {\n  ami           = <span class=\"hljs-string\">\"ami-12345678\"<\/span>\n  instance_type = <span class=\"hljs-string\">\"t2.micro\"<\/span>\n}\n\n<span class=\"hljs-comment\"># Step 2: Replace the resource block with the removed block<\/span>\nremoved {\n  from = aws_instance.example\n}\n\n<span class=\"hljs-comment\"># Step 3: Run Terraform commands<\/span>\nterraform plan\nterraform apply\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Step by Step in terraform To remove a resource from your Terraform configuration &amp; state file without destroying the real infrastructure object it manages Old Method &#8211; Using Command Line Here is a step-by-step guide to remove a resource from your Terraform configuration and state file without destroying the real infrastructure object: 1. Identify the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-47475","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=47475"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47475\/revisions"}],"predecessor-version":[{"id":47476,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47475\/revisions\/47476"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=47475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=47475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=47475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}