Linux / Unix rsync: Delete Source File After Transfer

See all UNIX related articles/faq
I would like to move all desktop:~/Downloads/*.avi files from my desktop to laptop:~/Download/ using the rsync command. How do I delete source file after successful transfer is done using
You need to pass the --remove-source-files option to the rsync command. It tells rsync to remove from the sending side the files (meaning non-directories) that are a part of the transfer and have been successfully duplicated on the receiving side. Do not pass the --delete option to rsync command as it delete extraneous files from destination directory.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix terminal
Category File Management
OS compatibility AIX AlmaLinux Alpine Amazon Linux Arch BSD CentOS Debian Fedora FreeBSD HP-UX Linux macOS Mint NetBSD OpenBSD openSUSE Pop!_OS RHEL Rocky Stream SUSE Ubuntu Unix WSL
Est. reading time 3 minutes
Advertisement

Delete source after successful transfer using rsync

WARNING! All of the options discussed below can be dangerous if misused! Therefore, trying a run using the --dry-run option is an excellent idea to see what files will be deleted. The nixCraft or author is not responsible for data loss.

The syntax is as follows:

rsync --dry-run -remove-source-files -options /path/to/src/ /path/to/dest
rsync --dry-run --remove-source-files -options /path/to/src/ computerB:/path/to/dest
rsync --dry-run --remove-source-files -av /path/to/src/*.avi computerB:/path/to/dest

You can always perform a trial run with no changes made using --dry-run option as follows:

rsync --dry-run --remove-source-files -azv /path/to/Download/*.avi laptop:~/Download

If you like output run the final command without --dry-run option. For example:

rsync --remove-source-files -azv /path/to/Download/*.avi laptop:~/Download

Deleting extraneous files from destination directories

The following command tells rsync to delete extraneous files from the receiving side (ones that aren’t on the sending side), but only for the directories that are being synchronized. You must have asked rsync to send the whole directory (e.g. “dir” or “dir/”) without using a wildcard for the directory’s contents (e.g. “dir/*”) since the wildcard is expanded by the shell and rsync thus gets a request to transfer individual files, not the files’ parent directory. Files that are excluded from the transfer are also excluded from being deleted unless you use the --delete-excluded option or mark the rules as only matching on the sending side. So, pass the --delete as follows:

rsync --dry-run --delete -azv source dest
rsync --dry-run --delete -azv /foo/bar/ destA:/foo/bar
############################################################################################
# Run the final command without '--dry-run' option when you got the correct outputs
#############################################################################################
rsync --delete -azv /foo/bar/*.txt destA:/foo/bar

The --delete option may be combined with one of the –delete-WHEN options without conflict, as well as –delete-excluded as follows:

Table 1: rsync delete related options
–delete-WHEN option Description
--delete-before Receiver deletes before transfer, not during
--delete-during Receiver deletes during the transfer
--delete-delay Find deletions during, delete after
--delete-after Receiver deletes after transfer, not during
--delete-excluded Also delete excluded files from destination dirs
--delete-missing-args Delete missing source args from destination
--ignore-errors Tells –delete to go ahead and delete files even when there are I/O errors.

Summing up

The --remove-source-files means the rsync command will remove from the sending side the files (meaning non-directories) that are a part of the transfer and have been successfully duplicated on the receiving side. On the other hand, --delete option tells rsync to delete extraneous files from the receiving side (ones that aren’t on the sending side) but only for the directories that are being synchronized. Both of these options can be dangerous if misused. First, it is a perfect idea to try a run using the --dry-run option to see what files will be deleted. For more info, see the rsync command manual online or offline using the man command/info command or help command:
$ man rsync
$ rsync --help
# filter outputs using grep command or egrep command #
$ rsync --help | grep -E -w -- '--(delete|remove)'

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

10 comments… add one
  • Vijay Sep 14, 2012 @ 5:17

    This is one of the good aspect of rsync. Additionally, you can use this too --delete-after.

    • Dan Mar 14, 2014 @ 6:33

      --delete-after will remove the destination files after the transfer, not the source files.

  • Jalal Hajigholamali Sep 15, 2012 @ 4:54

    Hi,
    Very nice article….
    thanks

  • Pankaj Jul 24, 2013 @ 6:13

    this is to delete source file what would be the command if we want to delete an old file from destination after rsync complete
    my scenario is
    rsync -e ‘ssh -p 22’ -Cavz user@remote_computer:/home/user/dbbackup /home/user/backup/
    but this command rsync new file but also keep the old file in my home/user//backup/
    I want to remove the old file just want newly copied file.

    Please help.

  • Matt Aug 8, 2013 @ 19:09

    actually its called –remove-sent-files now i think? Well my version was anyway

  • Bharat Mar 10, 2014 @ 8:18

    Please explain something about differential backup in rsync.

  • shgn Jan 11, 2015 @ 5:59

    Thank you. How to delete directory from source after sync beside –remove-source-files?

  • Tyee Cambron May 26, 2015 @ 2:02

    Wait, what is the -options perimeter for??? I don’t understand, because i didn’t see it in the ‘man rsync’ command on my macbook.

    • Andrew Jul 11, 2015 @ 11:11

      Tyee Cambron, the “-options” perimeter is not an actual perimeter. The author has added that in to show that that is where you put your options, such as avz. So “-options” would be replaced by “-avz”

  • Danny Apr 16, 2023 @ 7:50

    Thanks, i was copying and deleteing test files and directories and could not work out what i was doing wrong, problem solved

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.