> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gcore.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Automatic object removal via AWS CLI

> Gcore Object Storage lifecycle policies automatically delete objects after a set number of days using AWS S3 rules.

Lifecycle policies in Gcore Object Storage automatically delete objects after a configured number of days, following the [AWS S3 lifecycle configuration](https://docs.aws.amazon.com/AmazonS3/latest/userguide/how-to-set-lifecycle-configuration-intro.html) standard.

## Lifecycle policy logic

Object deletion is processed around midnight UTC. The table below shows when an object is deleted based on the scenario.

| Scenario | Upload date | Policy set | Expiration (days) | Deleted on |
| - | - | - | - | - |
| Policy exists before upload | Jan 1 | Before Jan 1 | 1 | Jan 3 |
| Policy set after upload | Jan 1 | After Jan 1 | 1 | Jan 3 |
| Policy removed | Jan 1 | Removed | — | Never |

The midnight UTC processing window means deletion happens at the next processing cycle after the expiration period ends — not at the exact moment the period expires.

## Lifecycle configuration elements

AWS CLI uses a JSON configuration file. The file structure is:

```json theme={null}
{
  "Rules": [
    {
      "ID": "...",
      "Prefix": "",
      "Status": "Enabled",
      "Expiration": {
        "Days": ...
      }
    }
  ]
}
```

| Field | Description |
| - | - |
| `ID` | Unique rule identifier — up to 255 characters, letters, digits, and underscores. |
| `Prefix` | Leave empty to apply the rule to the entire bucket; specify a folder name to scope the rule to that folder only. |
| `Status` | `"Enabled"` activates the rule; `"Disabled"` suspends it without deleting it. |
| `Expiration.Days` | Number of days after which objects are automatically deleted. |

## AWS CLI lifecycle management

AWS CLI provides three commands for managing lifecycle policies: `put-bucket-lifecycle` to apply a policy, `get-bucket-lifecycle-configuration` to verify it, and `delete-bucket-lifecycle` to remove it.

### Applying a lifecycle policy

**1. Create the configuration file**

Create a `lifecycle.json` file with the following content:

```json theme={null}
{
  "Rules": [
    {
      "ID": "one_day",
      "Prefix": "",
      "Status": "Enabled",
      "Expiration": {
        "Days": 1
      }
    }
  ]
}
```

| Parameter | Description |
| - | - |
| `one_day` | Rule ID. |
| `""` (empty prefix) | The rule applies to the entire bucket. |
| `"Enabled"` | The rule is active. |
| `1` | Objects are deleted after 1 day. |

> [!NOTE]
>
> To apply the rule to a specific folder, set `"Prefix"` to the folder name: `"Prefix": "deleteme/"`.

**2. Apply the policy**

Run the following command from the directory containing `lifecycle.json`, replacing the placeholder values:

```sh theme={null}
aws s3api put-bucket-lifecycle --bucket my-bucket --lifecycle-configuration file://lifecycle.json --endpoint-url=https://<storage-hostname>
```

| Parameter | Description |
| - | - |
| `my-bucket` | The name of the bucket. |
| `<storage-hostname>` | The storage endpoint hostname from the **Details** section in the [Gcore Customer Portal](https://portal.gcore.com) — all hostnames are listed in [S3 URLs](/storage/manage-object-storage/s3-service-urls-and-default-region-names). |

**3. Verify the configuration**

Run the following command to check the current lifecycle configuration:

```sh theme={null}
aws s3api get-bucket-lifecycle-configuration --bucket my-bucket --endpoint-url=https://<storage-hostname>
```

The response contains the applied policy as JSON.

### Deleting a lifecycle policy

Run the following command to remove the lifecycle policy from the bucket:

```sh theme={null}
aws s3api delete-bucket-lifecycle --bucket my-bucket --endpoint-url=https://<storage-hostname>
```

| Parameter | Description |
| - | - |
| `my-bucket` | The name of the bucket. |
| `<storage-hostname>` | The storage endpoint hostname. |

## Protect objects from deletion with Object Lock

To prevent objects from being deleted or overwritten, use S3 Object Lock. See [Protect objects with Object Lock](/storage/manage-object-storage/protect-objects-with-object-lock).
