How to delete non empty s3 bucket with terraform?

Viewed 10186

I want to delete my non empty s3 bucket created with terraform. I used force_destroy=true option as well but still i get

BucketNotEmpty: The bucket you tried to delete is not empty
status code: 409, request id: xxxx, host id: xxxxxxx

also the bucket was created with force_destroy option:

resource "aws_s3_bucket" "pipelineartifactstore" {
  bucket = "${var.prefix}-${var.namespace}-${var.stage}-pipeline-artifactstore"
  acl    = "private"
  force_destroy = true
  server_side_encryption_configuration {
    rule {
      apply_server_side_encryption_by_default {
        sse_algorithm = "AES256"
      }
    }
  }
  tags = var.default_tags
}
2 Answers

Some objects may be added before enable encryption or versioning so try to empty the bucket with AWS cli:

aws s3 rm s3://bucket-name --recursive

Then run Terraform apply again.

Since this is for Codepipeline I suppose.

There is a force_destroy option for buckets that can be set. When the value is true it will remove all items in the bucket and then remove the bucket too.

Once set, you'll be able to remove the resource and then run terraform apply and the bucket will be removed, no need for any additional CLI steps.

Documentation Link

Related