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.

πŸ‘‹ While you are here

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay