Automating S3 Bucket Cleanup: A Step-by-Step Guide

👋 Welcome to my Hashnode profile! I'm a passionate technologist with expertise in AWS, DevOps, Kubernetes, Terraform, Datree, and various cloud technologies. Here's a glimpse into what I bring to the table: 🌟 Cloud Aficionado: I thrive in the world of cloud technologies, particularly AWS. From architecting scalable infrastructure to optimizing cost efficiency, I love diving deep into the AWS ecosystem and crafting robust solutions. 🚀 DevOps Champion: As a DevOps enthusiast, I embrace the culture of collaboration and continuous improvement. I specialize in streamlining development workflows, implementing CI/CD pipelines, and automating infrastructure deployment using modern tools like Kubernetes. ⛵ Kubernetes Navigator: Navigating the seas of containerization is my forte. With a solid grasp on Kubernetes, I orchestrate containerized applications, manage deployments, and ensure seamless scalability while maximizing resource utilization. 🏗️ Terraform Magician: Building infrastructure as code is where I excel. With Terraform, I conjure up infrastructure blueprints, define infrastructure-as-code, and provision resources across multiple cloud platforms, ensuring consistent and reproducible deployments. 🌳 Datree Guardian: In my quest for secure and compliant code, I leverage Datree to enforce best practices and prevent misconfigurations. I'm passionate about maintaining code quality, security, and reliability in every project I undertake. 🌐 Cloud Explorer: The ever-evolving cloud landscape fascinates me, and I'm constantly exploring new technologies and trends. From serverless architectures to big data analytics, I'm eager to stay ahead of the curve and help you harness the full potential of the cloud. Whether you need assistance in designing scalable architectures, optimizing your infrastructure, or enhancing your DevOps practices, I'm here to collaborate and share my knowledge. Let's embark on a journey together, where we leverage cutting-edge technologies to build robust and efficient solutions in the cloud! 🚀💻
Managing S3 buckets with versioning enabled can become a daunting task, especially when dealing with a large number of objects and versions. In this blog post, we'll walk through the process of creating an S3 bucket with versioning, uploading some objects, and using a script to automate the deletion of all objects, versions, and delete markers in the bucket. This script is particularly useful for managing and cleaning up large numbers of objects efficiently.
Step 1: Create an S3 Bucket with Versioning
First, let's create an S3 bucket with versioning enabled.
#!/bin/bash
BUCKET_NAME="your-unique-bucket-name"
# Create the S3 bucket
aws s3api create-bucket --bucket $BUCKET_NAME --region us-east-1
# Enable versioning on the bucket
aws s3api put-bucket-versioning --bucket $BUCKET_NAME --versioning-configuration Status=Enabled
This script creates an S3 bucket and enables versioning, allowing us to keep track of different versions of the objects we upload.

Step 2: Upload Objects to the Bucket
Next, let's upload some objects to our newly created bucket.
#!/bin/bash
BUCKET_NAME="your-unique-bucket-name"
# Upload some objects
aws s3 cp file1.txt s3://$BUCKET_NAME/file1.txt
aws s3 cp file2.txt s3://$BUCKET_NAME/file2.txt
Step 3: Delete Objects, Versions, and Delete Markers
The following script automates the deletion of all objects, versions, and delete markers in the bucket.
#!/bin/bash
BUCKET_NAME="your-unique-bucket-name"
# List all versions and delete markers
aws s3api list-object-versions --bucket $BUCKET_NAME --query "Versions[].[Key, VersionId]" --output text > versions.txt
aws s3api list-object-versions --bucket $BUCKET_NAME --query "DeleteMarkers[].[Key, VersionId]" --output text > delete-markers.txt
# Delete all versions
while IFS=$'\t' read -r key version; do
if [ -n "$key" ] && [ -n "$version" ]; then
aws s3api delete-object --bucket "$BUCKET_NAME" --key "$key" --version-id "$version"
fi
done < versions.txt
# Delete all delete markers
while IFS=$'\t' read -r key version; do
if [ -n "$key" ] && [ -n "$version" ]; then
aws s3api delete-object --bucket "$BUCKET_NAME" --key "$key" --version-id "$version"
fi
done < delete-markers.txt
# Clean up
rm versions.txt delete-markers.txt
# Delete the bucket
aws s3api delete-bucket --bucket "$BUCKET_NAME"
Explanation of the Script
Let's break down what each part of the script does:
Set the Bucket Name
BUCKET_NAME="your-unique-bucket-name"This line defines a variable
BUCKET_NAMEwith the name of the S3 bucket you want to clean up.List All Versions and Delete Markers
aws s3api list-object-versions --bucket $BUCKET_NAME --query "Versions[].[Key, VersionId]" --output text > versions.txt aws s3api list-object-versions --bucket $BUCKET_NAME --query "DeleteMarkers[].[Key, VersionId]" --output text > delete-markers.txtThese commands list all the versions and delete markers in the bucket and save the output to two text files:
versions.txtanddelete-markers.txt. The--queryparameter is used to extract the keys and version IDs, and the--output textoption formats the output as plain text.Delete All Versions
while IFS=$'\t' read -r key version; do if [ -n "$key" ] && [ -n "$version" ]; then aws s3api delete-object --bucket "$BUCKET_NAME" --key "$key" --version-id "$version" fi done < versions.txtThis loop reads each line from
versions.txt, extracting the key and version ID. If both values are present, it uses theaws s3api delete-objectcommand to delete the specific version of the object.Delete All Delete Markers
while IFS=$'\t' read -r key version; do if [ -n "$key" ] && [ -n "$version" ]; then aws s3api delete-object --bucket "$BUCKET_NAME" --key "$key" --version-id "$version" fi done < delete-markers.txtSimilar to the previous loop, this loop reads from
delete-markers.txtand deletes each delete marker found.Clean Up
rm versions.txt delete-markers.txtThis command removes the temporary files
versions.txtanddelete-markers.txtto clean up after the script runs.Delete the Bucket
aws s3api delete-bucket --bucket "$BUCKET_NAME"Finally, this command deletes the now-empty S3 bucket.
Purpose and Use Case
Managing S3 buckets with versioning enabled can lead to a large number of object versions and delete markers. This script provides a simple and effective way to clean up these objects, making it ideal for scenarios where you need to manage storage costs or reorganize your bucket structure. By automating the deletion process, you can ensure that your bucket remains organized and free from unnecessary versions and delete markers.
Conclusion
In this blog post, we covered how to create an S3 bucket with versioning, upload objects, and use a script to delete all objects, versions, and delete markers. This script is particularly useful for managing large numbers of objects and maintaining an organized bucket structure.
Feel free to customize and adapt the script to suit your specific needs, and happy cleaning!




