---
title: "Object Versioning in S3"
description: "Enable object versioning for S3 object storage with Hostman guides. Keep track of changes and restore previous versions."
---

> For the complete documentation index for AI agents, see [llms.txt](https://hostman.com/llms.txt).

Versioning in S3 allows you to retain all changes made to objects uploaded to a bucket. It helps protect data from accidental deletion or overwrites and allows you to restore previous versions of objects when needed.

Versioning can be enabled using various clients, such as AWS CLI, S3 Browser, and others. This article covers working with versions using AWS CLI.

## Enabling Versioning

Versioning can be enabled through the dashboard or using clients such as AWS CLI, S3 Browser, and others.

### Using the Dashboard

1.  Go to the **S3 Storage** section and click on the bucket.
2.  Go to the **Settings** tab and click **Change** next to **Version history is disabled**.
    

![198b15da 7d64 460e Aff6 5bada42d5b26](https://content.hostman.com/assets/57386ace-b54d-459a-a679-e40b2c869396.png?width=1538&height=869)

3.  Confirm enabling versioning by clicking **Enable versioning**.
    

Note: Once enabled, versioning cannot be fully disabled for the bucket later; it can only be suspended.

### Using AWS CLI

To enable versioning on an S3 bucket using AWS CLI, run the following command:

```shell
aws s3api put-bucket-versioning --bucket <bucket_name> --versioning-configuration Status=Enabled --endpoint-url https://s3.hmstorage.net
```

To verify that versioning is enabled, use:

```shell
aws s3api get-bucket-versioning --bucket <bucket_name> --endpoint-url https://s3.hmstorage.net
```

Expected output:

```shell
{
    "Status": "Enabled",
    "MFADelete": "Disabled"
}
```

## Testing Versioning

Let's demonstrate how versioning works with an example.

1.  Create a test file:
    

```shell
echo "Version 1" > testfile.txt
```

2.  Upload it to the bucket:
    

```shell
aws s3 cp testfile.txt s3://<bucket_name>/ --endpoint-url https://s3.hmstorage.net
```

3.  Update the file:
    

```shell
echo "Version 2" > testfile.txt
```

4.  Upload it again:
    

```shell
aws s3 cp testfile.txt s3://<bucket_name>/ --endpoint-url https://s3.hmstorage.net
```

Now the file has multiple versions. To check them, use:

```shell
aws s3api list-object-versions --bucket <bucket_name> --prefix testfile.txt --endpoint-url https://s3.hmstorage.net
```

Example output:

```shell
{
    "Versions": [
        {
            "Key": "testfile.txt",
            "VersionId": "S7hpDH6F0RYfubNoTjin6hpBu5ewDj.",
            "IsLatest": true
        },
        {
            "Key": "testfile.txt",
            "VersionId": "pdFehsuCbaPz2hNh4b9DBndfEwzjTfH",
            "IsLatest": false
        }
    ]
}
```

#### Important Notes

-   Each object version takes up storage space in the bucket.
-   Object versions are not displayed in the dashboard or other S3 clients.
-   Even if an object is deleted, all its versions remain in the bucket.

## Working with Versions in the Dashboard

To view a list of object versions in the dashboard:

1.  Go to the **S3 Storage** section and click on the bucket.
2.  Open the **Objects** tab.
3.  Click the three dots next to the object and select **Version history**.
4.  Hover over a version to see available actions:
    -   **File link** generates a presigned URL for this object version.
    -   **Downloading** allows to download the selected version.
    -   **Restoring object version** makes the selected version the current version.
    -   **Deleting object version** (by clicking on the three dots) allows to delete the chosen version.

## Managing Object Versions

There are several commands available for managing object versions.

### List All Versions of a Specific Object

```shell
aws s3api list-object-versions --bucket <bucket_name> --prefix <object_name> --endpoint-url https://s3.hmstorage.net
```

### List All Objects and Versions

```shell
aws s3api list-object-versions --bucket <bucket_name> --query 'Versions[*].[Key, VersionId, Size]' --endpoint-url https://s3.hmstorage.net
```

### Downloading a Specific Object Version

To download a specific version of an object, use:

```shell
aws s3api get-object --bucket <bucket_name> \
                     --key testfile.txt \
                     --version-id pdFehsuCbaPz2hNh4b9DBndfEwzjTfH \
                     testlocalfile.txt \
                     --endpoint-url https://s3.hmstorage.net
```

Where:

-   `--key` – The object name in the bucket.
-   `--version-id` – The version ID (retrieved using `list-object-versions`).
-   `testlocalfile.txt` – The name of the file where the downloaded version will be saved.

### Restoring a Specific Object Version

If an object was updated or deleted, you can restore a previous version by copying it over the current one:

```shell
aws s3api copy-object --bucket <bucket_name> \
                      --key testfile.txt \
                      --copy-source <bucket_name>/<object_name>?versionId=<version_id> \
                      --endpoint-url https://s3.hmstorage.net
```

Where:

-   `<bucket_name>/<object_name>` – Full path to the object in the bucket.
-   `?versionId=<version_id>` – The version to be restored.

This command will create a new version in the bucket identical to the restored one.

### Deleting a Specific Object Version

To delete a specific version of an object, use:

```shell
aws s3api delete-object --bucket <bucket_name> --key <object_name> --version-id S7hpDH6F0RYfubNoTjin6hpBu5ewDj. --endpoint-url https://s3.hmstorage.net
```

Where `--version-id` is the ID of the object version to be deleted.

### Configuring Automatic Deletion of Old Versions

To prevent old file versions from accumulating, you can set up an [object lifecycle](https://hostman.com/docs/s3/supported-features/object-lifecycle/) policy.

1.  Create a file called `lifecycle.json`:
    

```shell
{
    "Rules": [
        {
            "ID": "DeleteOldVersions",
            "Status": "Enabled",
            "Filter": {},
            "NoncurrentVersionExpiration": {
                "NoncurrentDays": 30
            }
        }
    ]
}
```

This rule deletes object versions older than 30 days while keeping the current versions intact.

2.  Apply the lifecycle policy:
    

```shell
aws s3api put-bucket-lifecycle-configuration --bucket <bucket_name> --lifecycle-configuration file://lifecycle.json --endpoint-url https://s3.hmstorage.net
```

3.  Verify that the policy has been applied:
    

```shell
aws s3api get-bucket-lifecycle-configuration --bucket <bucket_name> --endpoint-url https://s3.hmstorage.net
```
