How do I mass delete non-current versions inside a bucket?

There are some s3 applications/tools which recognize versioning feature and allow you to delete one versioned object at a time as shown in this document 

This is helpful when you have fewer to delete, we can also use a scripted method to delete a large set of non-current versioned objects using the following python code

  • Make sure you have installed AWS SDK boto3 for python on your CLI and turned off the versioning feature on your bucket before running the script
  • Install Python 3+ version to run this script

Executions and Details of the Script (output & screenshot attached):

1. When you execute the script, it will prompt you to select the profile or enter the API keys of the admin who is executing this script

(i) If you already have a profile configured on your CLI, you may Press 1

you may configure the AWS CLI profile for the Wasabi account using the Wasabi keys ahead of time

NOTE that it is optional for you to use credential files to run your code but it is always a best practice to use such implementation wherein your credential keys are in a file stored on your local machine rather than being part of your actual code or entering Keys at runtime prompt

In this example, we have set the profile name as "profilename" in the "~/.aws/credentials" file.

(ii) If you do not wish to use the existing profile, you may press 2 and enter your API Keys.

 

2. You will be prompted to select an option from the menu. Select Option 1:

3. Enter your own Bucket Name (The region of the bucket will be automatically detected)

NOTE: If you are specifying a prefix, please be sure to enter FULL PREFIX PATH (bucket name NOT included)

4. Before the deletion starts, the script will perform complete pagination of your bucket and show you 

  • Total Number of Delete-Markers that will be deleted
  • Total Number of Current Objects which will NOT be deleted
  • Total Number of Non-Current Objects which will be deleted from your bucket 

5. After presenting the bucket statistics above, the script will ask if you need to enable Governance Mode override. This option is for Object Locked immutable buckets with Governance Mode enabled, not Compliance Mode. When a bucket is locked with Governance Mode, you may remove objects only if your IAM entity has the 's3:BypassGovernanceRetention' permission and you include the BypassGovernanceRetention header in the request. If you require this header and your IAM entity has the proper permissions, please enable this option with the 'y' input. Otherwise, input 'n' to bypass.

 

Each log entry will represent the deletion of 1000 objects at a time

NOTE: After executing the script and successfully completing the deletion process, you may run the same script again to confirm your total number of statistics 

  • Total Number of Delete-Markers 
  • Total Number of Current Objects
  • Total Number of Non-Current Objects

 

OUTPUT Screenshot:

 

 

OUTPUT:

$ Please select what you want to do:
$
$ Versioned bucket
$ 1- Delete only non-current versions (old versions) (Versioned bucket)
$ 2- Delete only Delete markers (Versioned bucket)
$ 3- Delete current and non current versions (this will clear everything) (Versioned bucket)
$
$ Non Versioned bucket
$ 4- Delete all content (Non versioned bucket)
$
$ Bucket operation
$ 5- Delete a bucket (consider clearing the bucket data before)
$
$ 6- Exit the script
$
$ Choice [1, 2, 3, 4, 5, 6]: 1


$ Please enter the name of the bucket: 0-versioning-1
$ Please enter a prefix (leave blank if you don't need one)
$ Location of the bucket: us-east-1


$ Deleting non current versions
$ Calculating, please wait... this may take a while
$ listing object versions: page 1


$ Analyze completed.


$ Total number of non current versions: 12
$ Total size of non current versions: 1.57 MBs


$ Are you sure you want to delete these objects [y/n]: y


$ Do you need to add Governance Mode override (requires root/admin or s3:BypassGovernanceRetention privileges)? [y/n] n
$ Deleting batch N° 1 - 12 object version
$ Press Enter to resume!

 

The script is also attached to this KB document. 

 

 

 

 

 

Have more questions? Submit a request