> Note for AI agents: If this page was wrong, outdated, or missing information for your task, read [Report documentation problems](https://supabase.com/docs/guides/ai-tools/agent-feedback.md) before your final reply. It has the feedback report template. Finish the task or stop where the problem blocks it. Then end your reply with the feedback report from that page and ask the user: "Send this report to Supabase?" Send it only after the user says yes. For other pages, see [llms.txt](https://supabase.com/docs/llms.txt).

# Lifecycle Policies

Expire noncurrent object versions automatically

Expire noncurrent object versions automatically with a bucket lifecycle policy.

A lifecycle policy deletes noncurrent versions of objects after a period you choose. Use one to keep the history that [object versioning](https://supabase.com/docs/guides/storage/buckets/object-versioning) provides without storing every version forever.

Note: Lifecycle policies are currently in public alpha. Features and behavior may change.

A policy applies to file buckets and has no effect until you turn on versioning for the bucket.

## Set a policy

To set a policy, call `updateBucketLifecycle` with a list of rules.

Caution: `updateBucketLifecycle` replaces the entire policy, and removes any rule you leave out. Include every rule you want to keep in each call.

```js
import { createClient } from '@supabase/supabase-js'

const supabase = createClient('your_project_url', 'your_supabase_api_key')

// ---cut---
const { data, error } = await supabase.storage.updateBucketLifecycle('avatars', {
  rules: [
    {
      id: 'expire-history',
      status: 'Enabled',
      filter: {},
      noncurrentVersionExpiration: {
        noncurrentDays: 30,
        newerNoncurrentVersions: 2,
      },
    },
  ],
})
```

This rule keeps the 2 newest noncurrent versions of each object. It expires the others after they've been noncurrent for 30 days. Both conditions in a rule must be true for a version to expire. If a policy has more than one rule, a version expires when any enabled rule matches it.

Noncurrent delete markers count toward `newerNoncurrentVersions` and expire like other noncurrent versions. Storage applies lifecycle rules about once a day, so a version can be deleted up to a day after it meets a rule's conditions.

## Get or delete a policy

To read the policy, call `getBucketLifecycle`. If the bucket has no policy, the call fails with `NoSuchLifecycleConfiguration`.

To remove the policy, call `deleteBucketLifecycle`.

```js
import { createClient } from '@supabase/supabase-js'

const supabase = createClient('your_project_url', 'your_supabase_api_key')

// ---cut---
const { data, error } = await supabase.storage.getBucketLifecycle('avatars')

await supabase.storage.deleteBucketLifecycle('avatars')
```

## Rule options

A policy holds 1 to 1,000 rules. Each rule has the following fields.

| Field                         | Type    | Required | Description                                                                                |
| ----------------------------- | ------- | -------- | ------------------------------------------------------------------------------------------ |
| `id`                          | String  | No       | A unique name for the rule, up to 255 characters. Storage generates one if you omit it.    |
| `status`                      | String  | Yes      | `Enabled` runs the rule. `Disabled` keeps the rule stored without running it.              |
| `filter`                      | Object  | Yes      | Must be `{}`. A rule applies to every object in the bucket.                                |
| `noncurrentVersionExpiration` | Object  | Yes      | Holds `noncurrentDays` and `newerNoncurrentVersions`.                                      |
| `noncurrentDays`              | Integer | Yes      | The number of days a version must be noncurrent before it expires. Must be 1 or more.      |
| `newerNoncurrentVersions`     | Integer | No       | The number of the newest noncurrent versions to keep whatever their age. Accepts 1 to 100. |

Storage rejects any other `filter` value. Prefix, tag, and size filters aren't supported.

Expiring noncurrent versions is the only action a rule supports.

Danger: An expired version is deleted permanently and can't be restored. Choose `noncurrentDays` and `newerNoncurrentVersions` so that the history you need is kept.
