Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

Terraform Tutorials: Step-by-Step Tutorial for Using the removed Block

Step-by-Step Tutorial for Using the removed Block in Terraform

The removed block in Terraform (introduced in version 1.7) 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.


What is the removed Block?

The removed block explicitly informs Terraform that certain resources are no longer managed by the configuration. During terraform apply, Terraform removes these resources from the state without attempting to delete them in the actual infrastructure.

removed {
  from = aws_instance.example

  lifecycle {
    destroy = false
  }
}Code language: JavaScript (javascript)


When to Use the removed Block

Here are 10 scenarios where the removed block is useful:

  1. Legacy Resource Cleanup: Resources created with Terraform are now managed manually or by another tool.
  2. Split Configurations: Moving resources from one Terraform workspace/module to another without impacting the infrastructure.
  3. Orphaned State Entries: Removing unused state entries that no longer exist in real infrastructure.
  4. Migration to a New Tool: Switching from Terraform to another IaC tool while leaving resources intact.
  5. Third-Party Resource Management: Transitioning resource management to another team or tool.
  6. Failed Deletions: When a resource deletion fails but you no longer want Terraform to track it.
  7. Resource Lock-In Prevention: Detaching resources that must remain after Terraform’s scope changes.
  8. State File Size Optimization: Removing large numbers of unmanaged resources from the state file to improve performance.
  9. Custom Manual Adjustments: Detaching resources that are being modified outside Terraform’s scope.
  10. Testing and Development: Temporarily removing a resource for testing purposes without actually deleting it.

How to Use the removed Block

Step-by-Step Process

  1. Identify the Resource to Remove
    Use terraform state list to find the resource in the Terraform state that you want to stop managing. terraform state list
  2. Add the removed Block
    Add a removed block to your configuration for the resource(s) you want to remove. removed { from = aws_instance.example }
  3. Run terraform plan
    Terraform will show that the specified resource will be removed from the state. terraform plan
  4. Apply the Changes
    Execute terraform apply to remove the resource from the state file. terraform apply
  5. Clean Up the Configuration
    After removing the resource from the state, you can remove the removed block if it is no longer needed.

5 Code Examples of the removed Block

Example 1: Removing a Single Resource

# Step 1: Identify the resource
resource "aws_instance" "example" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
}

# Step 2: Replace the resource block with the removed block
removed {
  from = aws_instance.example
}

# Step 3: Run Terraform commands
terraform plan
terraform apply
Code language: PHP (php)

Example 2: Removing Multiple Resources

# Step 1: Define multiple resources (before removal)
resource "aws_instance" "web" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
}

resource "aws_instance" "db" {
  ami           = "ami-87654321"
  instance_type = "t3.micro"
}

# Step 2: Add removed blocks for both resources
removed {
  from = aws_instance.web
}

removed {
  from = aws_instance.db
}

# Step 3: Run Terraform commands
terraform plan
terraform apply
Code language: PHP (php)

Example 3: Removing Dynamically Created Resources

# Step 1: Original resource definition
resource "aws_s3_bucket" "example" {
  for_each = toset(["bucket1", "bucket2", "bucket3"])
  bucket   = each.key
}

# Step 2: Replace the resource block with removed blocks
removed {
  from = aws_s3_bucket.example["bucket1"]
}

removed {
  from = aws_s3_bucket.example["bucket2"]
}

removed {
  from = aws_s3_bucket.example["bucket3"]
}

# Step 3: Run Terraform commands
terraform plan
terraform apply
Code language: PHP (php)

Example 4: Transitioning to a New Module

# Step 1: Original resource definition
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

# Step 2: Move VPC definition to a new module
module "network" {
  source     = "./modules/network"
  cidr_block = "10.0.0.0/16"
}

# Step 3: Use the removed block for the old VPC definition
removed {
  from = aws_vpc.main
}

# Step 4: Run Terraform commands
terraform plan
terraform apply
Code language: PHP (php)

Example 5: Removing Orphaned State Entries

# Step 1: Identify orphaned state entries
# List resources in the state
terraform state list

# Step 2: Add a removed block for the orphaned resource
removed {
  from = aws_security_group.orphaned
}

# Step 3: Run Terraform commands
terraform plan
terraform apply
Code language: PHP (php)

Best Practices for Using the removed Block

  1. Backup the State File: Always back up your terraform.tfstate file before making changes. cp terraform.tfstate terraform.tfstate.bak
  2. Document Changes: Clearly document why a resource was removed to ensure future maintainability.
  3. Test in Non-Production Environments: Verify the behavior of the removed block in a staging environment before applying it to production.
  4. Review Impact with plan: Always run terraform plan to validate that no unintended changes will occur.
  5. Remove the removed Block After Use: Once the resource is removed from the state, delete the removed block to avoid confusion in future updates.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Top 10 AI SEO Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, AI SEO tools have become indispensable for digital marketers, businesses, and content creators aiming to dominate search engine rankings. These tools leverage artificial intelligence…

Read More

Top 10 Product Lifecycle Management (PLM) Tools in 2026: Features, Pros, Cons & Comparison

Introduction Product Lifecycle Management (PLM) is a strategic approach to managing a productโ€™s journey from conception through design, manufacturing, and end-of-life. In 2026, PLM software has evolved…

Read More

Top 10 Patch Management Tools in 2026: Features, Pros, Cons & Comparison

Introduction: The Importance of Patch Management in 2026 In 2026, as cyber threats evolve and technology becomes more complex, patch management tools are critical for maintaining cybersecurity…

Read More

Top 10 Headless CMS Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, Headless Content Management Systems (CMS) have become the go-to solution for businesses seeking flexibility, scalability, and a modern approach to content management. Unlike traditional…

Read More

Top 10 AI Lead Scoring Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, AI lead scoring tools have become indispensable for B2B and B2C businesses aiming to optimize their sales pipelines. These tools leverage artificial intelligence to…

Read More

Top 10 AI Portfolio Optimization Tools in 2026: Features, Pros, Cons & Comparison

Introduction Investment management has always been about making smart choices at the right time. Traditionally, this required endless hours of research, manual calculations, and intuition. But in…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x