This utility script manages and deletes old versions of objects in an S3-compatible bucket. It is specifically designed for situations where versioning control cannot be disabled, such as with Backblaze B2. This script helps you delete versions more frequently than the default settings, aiding in better management of your storage space.
- Introduction
- Benefits
- Disclaimer
- Prerequisites
- Downloading the Script
- Usage
- License
- Contributing
- Support
- Automatically deletes old versions based on configurable filters
- Can target specific subsets of objects by prefix
- Easy to customize schedule with cron jobs
- Saves time compared to manually deleting old versions
- Helps reduce storage costs by pruning unused old versions
Before running the script, ensure you have AWS CLI v2 and jq installed on your system, and they are the latest versions or the versions compatible with the script.
Follow the official AWS guide to install AWS CLI v2 on your system or use the commands below:
# For Linux:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
Install jq using your system's package manager. For example, on Ubuntu/Debian:
sudo apt-get update
sudo apt-get install jq
And for Fedora/RHEL/CentOS:
sudo yum install jq
Verify Installation
Check the installations with:
aws --version
jq --version
To download the script, use the following wget
command:
wget https://github.com/Bortus-AI/Universal-S3-File-Version-Pruner/raw/main/s3prune.sh
chmod +x s3prune.sh
The script supports various configuration options to control its behavior, and it can be run in interactive or unattended modes. Use the --prefix
option to target a subset of objects within the bucket.
Customize the script's operation with command-line flags:
./s3prune.sh [--unattended] [--no-dry-run] [--bucket BUCKET] [--prefix PREFIX]
[--endpoint-url URL] [--max-keys MAXKEYS]
Run the script without arguments to enter interactive mode:
./s3prune.sh
For unattended mode, pass the --unattended
flag along with necessary parameters:
./s3prune.sh --unattended --bucket "your-bucket" --prefix "your-prefix" --endpoint-url "your-endpoint-url" --max-keys 1000
To disable dry run mode in unattended mode, use the --no-dry-run
flag:
./s3prune.sh --unattended --no-dry-run --bucket "your-bucket" --prefix "your-prefix" --endpoint-url "your-endpoint-url" --max-keys 1000
Use the --prefix
option to target a subset of objects in the bucket, e.g.:
--prefix folder1/subfolder/
The script deletes all old non-current object versions that match the prefix.
To schedule the script with cron, for example, to run at 2 AM daily:
0 2 * * * /path/to/s3prune.sh --unattended --no-dry-run --bucket "your-bucket" --prefix "your-prefix" --endpoint-url "your-endpoint-url" --max-keys 1000
An example of the script running in interactive mode with a dry run:
debian@vps-xxx:~$ ./s3prune.sh
Enter your bucket name [default-bucket]:
Enter your prefix (or hit enter for none) [default-prefix]:
Enter your endpoint URL [https://s3.default-endpoint.com]:
Enter max keys to delete per batch (default 1000) [1000]:
Enable dry run mode? (y/n) [y]:
Dry run: Would delete objects: {
"Objects": [
{
"Key": "example-folder/example-file.nfo",
"VersionId": "4_zxxxxxxxxxxxxxxx_fxxxxxxxxxxxxxxxb6_d2023xxxx_mxxxxxx_c005_vxxxxxxx_txxxx_u0xxxxxxxxxxx"
},
{
"Key": "another-folder/another-file.nfo",
"VersionId": "4_zxxxxxxxxxxxxxxx_fxxxxxxxxxxxxxxxd3_d2023xxxx_mxxxxxx_c005_vxxxxxxx_txxxx_u0xxxxxxxxxxx"
}
],
"Quiet": true
}
Deleted 2 old versions.
Dry run
enabled. No objects were actually deleted.
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
Contributions are welcome! Please submit pull requests with enhancements or fixes.
For assistance or to report issues, please open an issue on the GitHub repository page.