---
title: "How to automatically remove files from S3 using lifecycle rules defined in Terraform"
description: "How to define S3 lifecycle rules using Terraform"
author: "Bartosz Mikulski"
author_bio: "Principal AI Engineer & MLOps Architect. I bridge the gap between \"it works in a notebook\" and \"it works for 200 million users.\""
author_url: https://mikulskibartosz.name
author_linkedin: https://www.linkedin.com/in/mikulskibartosz/
author_github: https://github.com/mikulskibartosz
canonical_url: https://mikulskibartosz.name/terraform-s3-lifecycle-rules
---

When we want to remove old files from S3 automatically, we use the lifecycle rules, but I don't recommend setting them using the AWS Web Interface because, in my opinion, the whole infrastructure should be defined as code.

Thus, it is best to add a Terraform configuration for the bucket we want to clean.

We will need three things:

* the name of the bucket
* the key prefix of files we want to remove
* the number of days after which we want to clean the data

When we have all of that, we can define the lifecycle rule in Terraform:

```json
resource "aws_s3_bucket" "bucket" {
    bucket = "bucket_name"
    acl = "private"

    lifecycle_rule {
        id = "remove_old_files"
        enabled = true

        prefix = "key_prefix/"

        expiration {
            days = 180
        }
    }
}
```

In this example, I configured the expiration rule that removes files older than 180 days from the bucket `bucket_name`, but it applies only to the files which keys start with the prefix `key_prefix/`.

