Understand what rsync --delete-excluded removes
Distinguish sender exclusions from destination protection, and preview deletion before running a mirror.
--delete-excluded makes ordinary exclusion rules stop protecting matching files at the destination. With a recursive transfer, rsync can then delete those excluded destination files as well as other destination entries absent from the source file list.
The examples describe upstream rsync. Check rsync --version before applying them: a different implementation, such as an operating-system-provided openrsync, can behave differently. Test deletion rules on disposable directories with the exact executable you will use.
Compare the two behaviors
Consider a source directory with report.txt and debug.log, and a destination with those files plus old.txt:
rsync -av --delete --exclude='*.log' source/ destination/
The log is not transferred, but the ordinary exclusion also protects the destination's debug.log. The destination-only old.txt is eligible for deletion.
Now change the command:
rsync -av --delete-excluded --exclude='*.log' source/ destination/
The log remains excluded from the sender's file list, but loses the ordinary receiver-side protection. Both the destination log and old.txt can be deleted. --delete-excluded implies deletion; it is not limited to cleaning up the excluded extension.
This applies within the directory trees rsync is actually synchronizing. It is not an unrestricted search of every file anywhere below an arbitrary destination path. Explicit receiver-side protection rules can still prevent deletion.
Preview the complete operation
rsync -avni --delete-excluded --exclude='*.log' source/ destination/
Inspect both file updates and *deleting entries. Confirm the source and destination, the source trailing slash, and every filter before removing -n. The dry run predicts behavior for the filesystem state it examined; files changed between the preview and real run can change the result.
Deletion does not necessarily happen after copying. Rsync chooses or accepts a deletion algorithm such as delete-during, delete-before, or delete-after. Do not build a recovery strategy around an assumed order.
Keep some destination files protected
Use a specific transfer-root path where possible, such as /cache/, instead of a broad name that can match in many directories. To retain an explicitly protected destination file while deleting other excluded logs:
rsync -avni --delete-excluded \
--filter='P /keep.log' --exclude='*.log' source/ destination/
Here P is a receiver-side protect rule. Keep it before conflicting rules and test the exact directory layout. Splitting one broad exclude into several excludes does not restore destination protection under --delete-excluded.
If the task is only to remove selected old logs, a separately reviewed retention operation may be easier to reason about than changing an entire mirror's filter semantics. Keep a recoverable copy before any deletion operation involving valuable destination-only data.
Reference: rsync deletion and filter rules.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan