DEV Community

nomi3
nomi3

Posted on

How to Handle a Failed Deletion of Resources (Including Google Cloud Storage) in Terraform

When managing Google Cloud resources with Terraform, you may encounter a situation where destroying development environment resources fails because force_destroy is set to false on a Cloud Storage bucket. As a result, some resources remain in a partially deleted state. Here’s how to resolve this issue.

Versions

  • Terraform: 1.10.4
  • hashicorp/google: 6.16.0

Steps

First, set force_destroy to true on the relevant Cloud Storage bucket:

resource "google_storage_bucket" "example" {
  name     = "example-bucket"
  location = "US"
  // force_destroy = false
  force_destroy = true
}

resource "another_resource" "example" {
  name   = "example-object"
}
Enter fullscreen mode Exit fullscreen mode

Next, update only the Cloud Storage bucket by specifying it as the target. (If you run terraform apply without specifying a target, resources such as another_resource that were previously deleted will be recreated.)

terraform apply -target=google_storage_bucket.example
Enter fullscreen mode Exit fullscreen mode

Finally, run destroy again:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

By doing this, the Cloud Storage bucket and any remaining resources will be properly deleted.

Top comments (0)