← All posts
2 min read

Automatically abort stale multipart uploads

#aws#cli#s3#storage
📑 On this page

Part 51 of AWS from Zero. This lesson keeps the scope to one S3 behavior you can verify from the terminal.

What we are learning

AbortIncompleteMultipartUpload prevents forgotten upload parts from remaining indefinitely.

Before you run it

aws sts get-caller-identity
REGION="ap-south-1"
BUCKET="replace-with-your-private-demo-bucket"

Use a private general purpose bucket that you own. Replace every placeholder before running a write or delete command.

The command

cat > lifecycle-multipart.json <<'EOF'
{
  "Rules": [{
    "ID": "AbortStaleMultipartUploads",
    "Status": "Enabled",
    "Filter": {"Prefix": ""},
    "AbortIncompleteMultipartUpload": {"DaysAfterInitiation": 7}
  }]
}
EOF
 
aws s3api put-bucket-lifecycle-configuration \
  --bucket "$BUCKET" \
  --lifecycle-configuration file://lifecycle-multipart.json

A successful configuration command may return no output. Treat inspection as a separate required step.

Inspect the result

aws s3api get-bucket-lifecycle-configuration \
  --bucket "$BUCKET" \
  --query "Rules[].{ID:ID,Days:AbortIncompleteMultipartUpload.DaysAfterInitiation}" \
  --output table

Read the returned fields rather than assuming the write succeeded exactly as intended.

One tiny variation

aws s3api list-multipart-uploads \
  --bucket "$BUCKET" \
  --query "Uploads[].{Key:Key,Started:Initiated}" \
  --output table

Listing active uploads helps you choose a sensible cleanup window.

Common mistake

Lifecycle cleanup is asynchronous. It is a guardrail, not a replacement for explicitly aborting failed uploads in application error handling.

Cleanup

aws s3api delete-bucket-lifecycle --bucket "$BUCKET"
rm lifecycle-multipart.json

Remove the demo rule or merge it into the bucket's intended lifecycle configuration.

Next, we will learn Audit all S3 lifecycle rules.

Official AWS CLI reference