How to Use AWS S3 Sync with File Deletion

If you’re using AWS S3 and the ‘aws s3 sync’ command, you may be curious about how it handles file deletions. This command is quite versatile and can be customized to fit your needs, including the ability to delete files that aren’t in the source directory anymore.

By adding the ‘–delete’ flag when you run ‘aws s3 sync’, you instruct AWS to remove files from the destination that no longer exist in the source. This feature is great for keeping your S3 bucket aligned with your local files or another S3 bucket. However, it’s important to use this option cautiously, as it can lead to unintended data loss. Always review your command thoroughly before you hit enter.

On the other hand, if you choose not to use the ‘–delete’ flag, S3 will only add or update files, leaving any additional files in the destination untouched. This can be useful if you want to keep certain files that might have been removed from your source. Have you worked with this command in your projects? What steps do you take to safeguard against accidental deletions?