Accidental deletion, ransomware, and a bad deployment script can wipe out critical data in seconds. If your Amazon S3 bucket holds backups, compliance records, or application data, a single delete request should never be the end of the story. That is exactly the problem that aws s3 versioning object lock solves: together, these two features keep every version of an object and make selected versions impossible to delete or overwrite until a retention period expires.

In this guide, you will learn how to enable S3 Versioning and Object Lock on a new bucket, configure retention modes, upload and protect objects, and test that deletion is actually blocked. You will also learn the common mistakes that leave buckets unprotected even after these features appear to be turned on.
What Is AWS S3 Versioning Object Lock?
Versioning and Object Lock are two separate features that work best together. When Versioning is enabled, S3 keeps every version of an object instead of overwriting it. Deleting an object only adds a delete marker; the previous versions remain recoverable until you explicitly delete each version.
Object Lock adds WORM (Write Once Read Many) protection on top of Versioning. Once an object version is locked, it cannot be deleted or overwritten for a defined retention period, even by the bucket owner or an administrator with full S3 permissions. This is the same principle behind immutable backups: the data is protected from both accidents and malicious actors, including compromised credentials.
Object Lock offers two retention modes:
- Governance mode: Protected users cannot overwrite or delete a version, but users with the special
s3:BypassGovernanceRetentionpermission can still remove it. This mode suits internal policies where an administrator may need an emergency override. - Compliance mode: No user, including the root account, can delete or shorten the retention period of a protected version until it expires. This mode is designed for regulatory requirements such as financial records or legal holds.
There is also a Legal Hold, which is independent of any retention date. A legal hold blocks deletion indefinitely until it is explicitly removed by an authorized user. You can combine a legal hold with a retention period for maximum protection.
Prerequisites
- An AWS account with permissions to create S3 buckets and manage Object Lock settings.
- AWS CLI version 2 installed and configured, or access to the AWS Management Console.
- A new S3 bucket, or a plan to create one. Object Lock can only be enabled when a bucket is created; you cannot enable it on an existing bucket that was created without it.
- A clear retention policy: decide how long each type of data must be kept before you start uploading.
Step 1: Create a Bucket with Object Lock Enabled
Object Lock must be enabled at bucket creation time. This is the single most common mistake: administrators enable Versioning on an old bucket and assume Object Lock is available, but it is not. Create a fresh bucket for protected data.
Using the AWS CLI:
aws s3api create-bucket \
--bucket my-protected-data-bucket \
--region us-east-1 \
--object-lock-enabled-for-bucket
For regions other than us-east-1, add the location constraint:
aws s3api create-bucket \
--bucket my-protected-data-bucket \
--region eu-west-1 \
--create-bucket-configuration LocationConstraint=eu-west-1 \
--object-lock-enabled-for-bucket
In the console, the same option appears under “Object Lock” in the bucket creation wizard. Once the bucket exists, verify Object Lock is enabled:
aws s3api get-object-lock-configuration \
--bucket my-protected-data-bucket
Step 2: Enable Versioning on the Bucket
Object Lock requires Versioning, and enabling Object Lock at creation time does not automatically turn Versioning on in every workflow. Enable it explicitly:
aws s3api put-bucket-versioning \
--bucket my-protected-data-bucket \
--versioning-configuration Status=Enabled
Confirm the status:
aws s3api get-bucket-versioning \
--bucket my-protected-data-bucket
With Versioning enabled, every upload creates a new version ID. Keep in mind that old versions consume storage, so pair Versioning with a lifecycle policy that cleans up noncurrent versions after a sensible period. For cost control strategies, see our guide on AWS S3 lifecycle rules to control storage costs.
Step 3: Set a Default Retention Rule
A default retention rule automatically applies a retention period to every new object version uploaded to the bucket. This is the safest approach for compliance buckets, because protection does not depend on each uploader remembering to set retention manually.
The following example sets a 365-day retention period in Compliance mode:
aws s3api put-object-lock-configuration \
--bucket my-protected-data-bucket \
--object-lock-configuration '{
"ObjectLockEnabled": "Enabled",
"Rule": {
"DefaultRetention": {
"Mode": "COMPLIANCE",
"Days": 365
}
}
}'
For internal data where an administrator may need an emergency override, use Governance mode instead and grant the bypass permission only to a tightly controlled IAM role. Never grant the bypass permission to broad administrator groups; doing so defeats the purpose of the lock.
Step 4: Upload Objects with Retention Settings
When the default retention rule is in place, a normal upload is automatically protected. You can also set retention explicitly per object:
aws s3api put-object \
--bucket my-protected-data-bucket \
--key backups/database-2026-10-27.sql \
--body ./database-2026-10-27.sql \
--object-lock-mode COMPLIANCE \
--object-lock-retain-until-date 2027-10-27T00:00:00Z
To apply a legal hold to a specific version, first note its version ID from the upload output, then run:
aws s3api put-object-legal-hold \
--bucket my-protected-data-bucket \
--key backups/database-2026-10-27.sql \
--version-id EXAMPLE_VERSION_ID \
--legal-hold Status=ON
For the full official reference on retention dates, modes, and legal holds, see the Amazon S3 Object Lock documentation.
Step 5: Test That Protection Actually Works
Never assume a lock works. Test it in a non-production bucket first, using a short retention period:
aws s3api delete-object \
--bucket my-protected-data-bucket \
--key backups/database-2026-10-27.sql
If the object version is locked, the delete request fails with an AccessDenied error. That failure is the expected, correct result. Also test the versioned behavior: with Versioning enabled, a delete request on an unlocked object only adds a delete marker, and you can restore the object by deleting the marker:
aws s3api list-object-versions \
--bucket my-protected-data-bucket \
--prefix backups/
aws s3api delete-object \
--bucket my-protected-data-bucket \
--key backups/database-2026-10-27.sql \
--version-id EXAMPLE_DELETE_MARKER_ID
Schedule this test as part of your backup verification routine. An untested backup is not a backup, and an untested lock is not protection.
How AWS S3 Versioning Object Lock Fits a Backup Strategy
Immutable storage is one layer of a defense-in-depth backup strategy, not a replacement for the whole strategy. A practical setup combines aws s3 versioning object lock with the following practices:
- Cross-region replication: Replicate protected objects to a second region so a regional outage does not take your backups with it.
- Version cleanup: Use lifecycle rules to expire noncurrent versions that are no longer needed, but never set a rule that deletes versions still inside their retention period. S3 will refuse to delete locked versions, which is by design.
- Least-privilege IAM: Application credentials should only have permission to upload, never to delete or bypass retention. Separate the roles used for writing backups from the roles used for administration.
- Monitoring: Enable CloudTrail data events for the bucket and alert on delete attempts, retention changes, and legal hold removals. A spike in denied deletes may indicate compromised credentials or ransomware activity.
Troubleshooting
Object Lock cannot be enabled on my existing bucket
This is expected behavior. Object Lock can only be enabled at bucket creation. Create a new bucket with Object Lock enabled, copy the objects over, and update your applications to use the new bucket. There is no workaround for an existing bucket.
Delete requests still succeed
Check three things. First, confirm the object version actually has a retention period or legal hold, not just the bucket default. Second, confirm you are deleting the version ID, not just the key, because a key-level delete only adds a delete marker. Third, in Governance mode, check whether the caller has the bypass permission; if so, the delete is allowed by design.
Storage costs grow unexpectedly
Versioning keeps every version, and locked versions cannot be deleted early. Review your lifecycle rules for noncurrent versions, and check whether applications are uploading full files instead of deltas. Use S3 Storage Lens to identify which prefixes are growing fastest.
Retention period is too long for a test object
In Compliance mode, a retention period cannot be shortened, even by the root account. This is intentional. Always test with short retention periods in a dedicated test bucket, and only apply long retention to production buckets after the policy is approved.
Conclusion
Combining Versioning with Object Lock turns an S3 bucket from ordinary storage into tamper-resistant storage. Versioning protects you from accidental overwrites, while Object Lock protects you from deletion, including deletion by compromised credentials or ransomware. The setup takes minutes, but the key decisions deserve care: create a dedicated bucket with Object Lock enabled from the start, choose Governance or Compliance mode deliberately, set a default retention rule, and test that deletion is actually blocked. With aws s3 versioning object lock in place and verified, your backups and compliance records gain a level of protection that no IAM policy alone can provide.