How do I make rsync delete files that have been deleted from the source folder?
To delete files in the target, add the --delete
option to your command. For example:
rsync -avh source/ dest/ --delete
NB: -avh
is for --archive --verbose --human-readable
The rsync command wont delete any file while you use some of its options delete
in that command. So if any file or folder added in source, it'll be synced to target without any deletion.
I suggest you to use rsync
for make backup from source files and use find ... rm
for deletion files for period of time or size of files:
rsync [options] SOURCE TARGET
find TARGET -maxdepth 1 -type f -mtime +60 -exec rm -f {} \;
The above code block, make a backup from source and then delete every files which last modified time are more than 2 month.
UPDATE
As I find that the delete
options are just for TARGET that if some files are removed from source, rsync --delete
remove them from TARGET. And the delete
option by after
and before
, as mentioned in its man page:
--delete-before receiver deletes before transfer, not during
Means that:
- rsync delete the file from TARGET which are removed from SOURCE.
- rsync start syncing files.
--delete-after receiver deletes after transfer, not during
Means that:
- rsync start syncing files.
- rsync delete the file from TARGET which are removed from SOURCE after syncing.
NOTE: The --delete-{before/after}
implement just in TARGET.