What exactly will --delete-excluded do for rsync?

Better Stack Team
Updated on November 23, 2023

The --delete-excluded option in rsync specifies that you want to delete files from the destination that are excluded from the synchronization. It has a specific behavior when combined with the --exclude option, which allows you to specify patterns or rules for excluding files or directories from the synchronization.

When you use --delete-excluded with rsync, the following happens:

  1. Exclusion Rules - You may have specified exclusion rules using the -exclude option in your rsync command. These rules define what files or directories should be excluded from the synchronization.
  2. Synchronization - rsync performs the synchronization according to your command, including copying files and directories from the source to the destination.
  3. Deletion - After the synchronization is complete, -delete-excluded comes into play. It instructs rsync to go through the destination directory and delete any files or directories that were excluded in the source during the synchronization process.

This means that if there were files or directories in the destination that matched the exclusion rules set in the source, rsync will remove them, effectively mirroring the exclusions from the source to the destination.

For example, if you have a command like this:

 
rsync -av --exclude='*.log' source_directory/ destination_directory/

And you use --delete-excluded, any files with a .log extension in the destination directory will be deleted if they exist there but are excluded from the source_directory.

Keep in mind that the --delete-excluded option should be used with caution, as it can lead to data loss if not used carefully. Make sure your exclusion rules are well-defined and that you have a clear understanding of what will be deleted. Always take extra precautions when working with the --delete-excluded option to avoid unintentional data loss.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.