Understanding rsync’s –delete-excluded: How It Affects Source vs Destination Files


2 views

Many sysadmins encounter confusion when first using rsync's --delete-excluded flag. The man page description ("also delete excluded files from dest dirs") can be ambiguous, especially regarding which side (source or destination) gets affected.

When using --delete-excluded with rsync:

  • It only affects the destination directory
  • Files/directories excluded via --exclude patterns will be deleted from the destination
  • No changes occur on the source server
rsync -avz --delete --exclude='tmp/' --delete-excluded \
user@source:/data/ /backups/source_data/

In this case:

  1. Any tmp/ directories in /backups/source_data/ will be deleted
  2. The source server's /data/tmp/ remains untouched
  3. All other files sync normally

The flag is particularly useful for:

  • Maintaining clean backup destinations
  • Preventing accumulation of excluded temporary files
  • Creating space-efficient backups by excluding non-critical data

To test before actual deletion:

rsync -avzn --delete --exclude='cache/' --delete-excluded \
user@web01:/var/www/ /backups/web01/

The -n (dry-run) flag shows what would happen without making changes.

For more control, consider using --filter rules instead of simple excludes:

rsync -av --delete --filter='exclude uploads/' --filter='protect important.txt' \
user@app01:/data/ /backups/app01/

When using rsync with --delete-excluded, this option specifically targets files/directories in the destination that match your exclusion patterns. It's crucial to understand this operates on the receiving side, not the source.

rsync -a --delete --delete-excluded /source/ user@remote:/destination/

Consider this common backup command:

rsync -avz --delete --exclude='tmp/' --delete-excluded \
  root@webserver:/var/www/ /backups/web/

Here's what happens:

  • Files in /var/www/ sync to /backups/web/
  • Any tmp/ directories on the destination get deleted
  • Files deleted from source get removed from destination
  • But /var/www/tmp/ remains untouched on the webserver

To verify behavior before executing:

rsync --dry-run --delete --exclude='logs/' --delete-excluded \
  user@prod:/data/ /backups/prod/

Key protection measures:

  • Always test with --dry-run first
  • Consider using --backup --backup-dir for critical data
  • Maintain proper permissions to prevent accidental deletions

For space-saving backups without permanent deletions:

rsync -av --exclude='*.tmp' --delete-excluded \
  --backup --backup-dir=/backups/deleted_files \
  /source/ /destination/

When --delete-excluded doesn't behave as expected:

  1. Verify exclusion patterns match exactly (case-sensitive)
  2. Check for trailing slashes in directory exclusions
  3. Ensure no conflicting filter rules exist
# Example showing precise pattern matching
rsync -av --exclude='/specific_dir/' --delete-excluded \
  /source/ user@remote:/dest/