Skip to content

How can I delete old versions of files in an S3 bucket?

0

How can I delete old versions of files in an S3 bucket? I enabled versioning in S3 but now I see multiple versions of the same file. What’s the recommended way to clean up older versions without affecting the latest one?

1 Answer
1
Accepted Answer

There are several ways to delete old versions of files in an S3 bucket without affecting the latest version:

  1. Using S3 Lifecycle Rules (Recommended):
  • Create a lifecycle rule that applies to your objects
  • Enable "Permanently delete noncurrent versions of objects"
  • Specify the number of days after objects become noncurrent
  • Optionally, specify the number of newer versions to retain
  • This approach is ideal for large buckets as it scales well and handles all versioning complexities
  1. Using the S3 Console:
  • Navigate to your bucket and enable "Show versions"
  • Select the check box next to the Version ID for the older versions you want to delete
  • Choose "Delete" and confirm by typing "permanently delete"
  • This approach works well for selectively deleting specific versions
  1. Using AWS CLI/API:
  • For specific versions, use the delete-object command with the version ID
  • For bulk deletion of versions under a specific path, you can use a command that combines list-object-versions with delete-objects

Remember that when you delete an object without specifying a version ID in a versioning-enabled bucket, S3 doesn't actually delete the object - it just adds a delete marker, making the object appear deleted. To permanently remove old versions, you must either specify the version ID or use lifecycle rules.

The lifecycle rule approach is generally recommended for ongoing management of versioned objects, especially for buckets with many objects, as it automates the cleanup process according to your retention policies.
Sources
Deleting object versions from a versioning-enabled bucket - Amazon Simple Storage Service
Troubleshooting versioning - Amazon Simple Storage Service
How to all objects in s3 ( and all its versions) with aws command line? | AWS re:Post
Deleting Amazon S3 objects - Amazon Simple Storage Service

answered 8 months ago
EXPERT
reviewed 8 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.