Use S3 sync delete without surprises
📑 On this page
Part 56 of AWS from Zero. This lesson keeps the scope to one S3 behavior you can verify from the terminal.
What we are learning
sync --delete makes the destination resemble the source by deleting destination-only keys. It is powerful enough to remove valid remote data.
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
aws s3 sync local-site/ "s3://$BUCKET/site/" \
--delete \
--dryrunRead both upload and delete lines. The dry run must use the same source, destination, and filters as the real command.
Inspect the result
aws s3api list-objects-v2 \
--bucket "$BUCKET" \
--prefix site/ \
--query "Contents[].Key" \
--output tableRead the returned fields rather than assuming the write succeeded exactly as intended.
One tiny variation
aws s3 sync local-site/ "s3://$BUCKET/site/" \
--delete \
--exclude "uploads/*" \
--dryrunExclude a destination-managed subtree so the sync does not consider it for deletion.
Common mistake
Reversing source and destination changes what gets deleted. Put the bucket and prefix in variables and print them before any real --delete run.
Cleanup
# No real sync is required for this lesson.
aws s3api list-objects-v2 --bucket "$BUCKET" --prefix site/If you intentionally ran the real sync, restore deleted content from versioning or backup according to your recovery plan.
Next, we will learn Configure an S3 static website index.