Search code examples
google-cloud-platformterraformbucket

Configure retention_policy for gcp storage bucket using terraform


I am trying to configure my google storage bucket to have a retention policy of 1 day. Anything older than a day should get deleted from the bucket.

I've added the following policy to my gcp bucket using terraform last week. When I go to gcp console it still shows files which are older than a day.

resource "google_storage_bucket” "test_bucket_name” {
  name          = "test-backups"
  location      = "US"
  force_destroy = false

retention_policy {
  retention_period = 86400
}

I want to know if I am missing any other configuration options. Thanks for your help!


Solution

  • retention_policy does not delete the objects automatically. It is only set as to ensure that the objects don't get deleted, overwritten, or archived within the period given.

    To delete the object after 24 automatically, you have to use lifecycle_rule. For example:

      lifecycle_rule {
        condition {
          age = "1"
        }
        action {
          type = "Delete"
        }
      }