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.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay