> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Removing Resources with the removed Block

> Explains Terraform removed block to safely remove resources from state without destroying infrastructure, providing workflow steps, examples, use cases, and differences from the moved block.

In this lesson we explain Terraform's `removed` block: what it does, when to use it, and the correct workflow to remove resources from Terraform state without destroying the underlying infrastructure.

Real-world scenario: production databases
You've been managing two production PostgreSQL instances with Terraform for months. The database team now needs full manual control of schema changes on one instance (for example, `production-db-02`) and asks that Terraform stop managing that instance so they can make manual changes safely.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/urHSIc4JjzJnqdR4/images/HashiCorp-Certified-Terraform-Associate-004/Refactoring-Terraform-State/Removing-Resources-with-the-removed-Block/real-world-scenario-database-icons.jpg?fit=max&auto=format&n=urHSIc4JjzJnqdR4&q=85&s=3db79618fa0905d15be4f8ceb661d696" alt="The image depicts a &#x22;Real-World Scenario&#x22; with a purple background, featuring an abstract icon above two stacked database symbols labeled &#x22;production_db_01&#x22; and &#x22;production_db_02&#x22;." width="1920" height="1080" data-path="images/HashiCorp-Certified-Terraform-Associate-004/Refactoring-Terraform-State/Removing-Resources-with-the-removed-Block/real-world-scenario-database-icons.jpg" />
</Frame>

Original Terraform resource definitions
These were the original resource blocks in your configuration:

```hcl theme={null}
resource "google_sql_database_instance" "prd_db_1" {
  name             = "production-db-01"
  database_version = "POSTGRES_15"
  region           = "us-central1"
  settings {
    tier = "db-f1-micro"
  }
}

resource "google_sql_database_instance" "prd_db_2" {
  name             = "production-db-02"
  database_version = "POSTGRES_15"
  region           = "us-central1"
  settings {
    tier = "db-f1-micro"
  }
}
```

Why you can't just delete the resource block
Removing the `prd_db_2` resource block from code and running `terraform plan` will make Terraform interpret the resource as removed from configuration and propose to destroy the real infrastructure:

```plaintext theme={null}
# Example terraform plan output
-/+ google_sql_database_instance.prd_db_2
  - resource will be destroyed
```

That would delete production data — which you must avoid. The goal is to make Terraform "forget" the resource (remove it from state) while leaving the actual database intact. The `removed` block provides a safer, declarative way to do this.

What the `removed` block does

* Removes the specified resource address from Terraform state so Terraform stops managing it.
* Leaves the actual infrastructure running and untouched.
* Is useful for handing resources off to other teams, transitioning to manual management, or splitting a monolithic configuration.

Core pieces to know

* `from` — specifies the resource address to remove from state. Use the resource address exactly as it appears in configuration (for example, `google_sql_database_instance.prd_db_2`).
* `removed` blocks only need the `from` argument. They instruct Terraform to remove the matching resource from state without destroying the underlying infrastructure.
* Always validate behavior in your Terraform version and provider. See HashiCorp's refactoring docs for details: [https://developer.hashicorp.com/terraform/language/state/refactoring](https://developer.hashicorp.com/terraform/language/state/refactoring)

Example `removed` blocks for the two databases:

```hcl theme={null}
removed {
  from = google_sql_database_instance.prd_db_1
}

removed {
  from = google_sql_database_instance.prd_db_2
}
```

<Callout icon="lightbulb" color="#1CB2FE">
  When using a `removed` block, the resource definition must be absent (or commented out) from your configuration first. Terraform will refuse to remove a resource from state if that resource still exists in code.
</Callout>

Important ordering and workflow
The sequence of steps matters. If you keep the resource block in code while also declaring a `removed` block for it, Terraform will be unable to reconcile them. Follow this workflow:

1. Delete or comment out the resource blocks you want Terraform to stop managing.
2. Add a `removed` block for each resource to be forgotten.
3. Run `terraform plan` to inspect the proposed state change.
4. Run `terraform apply` to update the state file and remove the resource from Terraform state.
5. After the state is updated, delete the temporary `removed` blocks from your configuration (they are not required after apply but can be kept for documentation).

Example commands:

```bash theme={null}
# Inspect changes (safe)
terraform plan

# Apply the state-only changes
terraform apply
```

The typical refactoring lifecycle is:
Delete (resource block) → Write (`removed`/`moved` block) → Plan → Apply → Delete (temporary scaffolding).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/urHSIc4JjzJnqdR4/images/HashiCorp-Certified-Terraform-Associate-004/Refactoring-Terraform-State/Removing-Resources-with-the-removed-Block/refactoring-workflow-terraform-steps.jpg?fit=max&auto=format&n=urHSIc4JjzJnqdR4&q=85&s=04ebb61c1104fe2a9b75eb5886abdbe0" alt="The image illustrates a refactoring workflow consisting of five steps: Write, Delete, Plan, Apply, and Delete, each with corresponding actions related to Terraform configuration." width="1920" height="1080" data-path="images/HashiCorp-Certified-Terraform-Associate-004/Refactoring-Terraform-State/Removing-Resources-with-the-removed-Block/refactoring-workflow-terraform-steps.jpg" />
</Frame>

`removed` vs `moved` — quick guidance

* Use `moved` when refactoring addresses within Terraform (renaming resources, moving to modules, etc.). `moved` updates state to reflect a change in resource address while leaving infrastructure intact.
* Use `removed` when you want Terraform to stop managing a resource entirely (hand-off to another team, manual management, or transferring to another state/workspace). `removed` deletes the resource from state without deleting the actual infrastructure.

Comparison table

| Action                                                  | Use case                                          | Terraform block             | Result                                                                          |
| ------------------------------------------------------- | ------------------------------------------------- | --------------------------- | ------------------------------------------------------------------------------- |
| Rename or re-address resource                           | Refactoring code, moving resources into modules   | `moved`                     | State updated to new addresses; infrastructure unchanged                        |
| Stop Terraform management / hand off resource           | Transferring ownership; manual management         | `removed`                   | Resource removed from state; infrastructure unchanged                           |
| Remove resource from configuration without state change | Temporary removal from config while still managed | None (use careful planning) | Terraform will propose to delete resource unless you remove it from state first |

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/urHSIc4JjzJnqdR4/images/HashiCorp-Certified-Terraform-Associate-004/Refactoring-Terraform-State/Removing-Resources-with-the-removed-Block/moved-block-removed-block-comparison.jpg?fit=max&auto=format&n=urHSIc4JjzJnqdR4&q=85&s=3b7ca392d3add88206e5281f11646c56" alt="The image compares the differences between a &#x22;moved block&#x22; and a &#x22;removed block&#x22; in the context of code management, highlighting the implications of each action." width="1920" height="1080" data-path="images/HashiCorp-Certified-Terraform-Associate-004/Refactoring-Terraform-State/Removing-Resources-with-the-removed-Block/moved-block-removed-block-comparison.jpg" />
</Frame>

Real-world use cases

* Hand off resources to another team: remove them from your state so another team can import them into theirs.
* Migration from another IaC tool: import resources into Terraform and `removed` or `moved` as part of a staged migration.
* Split monolithic configurations or separate environments: remove resources from one state and import them into another workspace or project.

Decision guidance diagram

* If you are renaming or reorganizing within Terraform → use `moved`.
* If you are transferring ownership or stopping Terraform management → use `removed`.
* Migration scenarios often combine `import` with `removed`/`moved` actions depending on the direction of transfer.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/urHSIc4JjzJnqdR4/images/HashiCorp-Certified-Terraform-Associate-004/Refactoring-Terraform-State/Removing-Resources-with-the-removed-Block/terraform-decision-tree-purple-background.jpg?fit=max&auto=format&n=urHSIc4JjzJnqdR4&q=85&s=ed157d6b4a108b677fd8f4b9ef4911f1" alt="The image is a decision tree on a purple background, detailing how to select the appropriate block for Terraform tasks such as renaming resources or handing off to another team." width="1920" height="1080" data-path="images/HashiCorp-Certified-Terraform-Associate-004/Refactoring-Terraform-State/Removing-Resources-with-the-removed-Block/terraform-decision-tree-purple-background.jpg" />
</Frame>

Warnings and best practices

<Callout icon="warning" color="#FF6B6B">
  Do not leave a resource block in your configuration while also declaring a `removed` block for the same address. Terraform will not be able to reconcile them and the operation will fail. Always test `removed` workflows in non-production environments first.
</Callout>

Final notes and references

* The `removed` block is a powerful, but intentionally infrequent, tool. Use it when you need Terraform to forget a resource while keeping the physical resource intact.
* Practice this workflow in a safe environment and double-check behavior for your Terraform version and provider.

Useful links

* Terraform state refactoring: [https://developer.hashicorp.com/terraform/language/state/refactoring](https://developer.hashicorp.com/terraform/language/state/refactoring)
* Terraform state commands overview: [https://developer.hashicorp.com/terraform/cli/commands/state](https://developer.hashicorp.com/terraform/cli/commands/state)

That wraps up this lesson on the `removed` block. Use it when you need Terraform to stop tracking a resource without deleting the actual infrastructure.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/hashicorp-certified-terraform-associate-004/module/42da48a6-09fe-43c7-997f-255cdb3e0c80/lesson/a0235061-b411-492a-98d9-5e9202ac8e23" />
</CardGroup>
