rsync - 10 examples in 11 days (Day 05)
Tags: linux, rsync, bash, tutorial
Published: 2020-07-21 02:23:42

# Day 05

> 10 examples in 11 days

## Delete

Today is the turn of the **--delete** option. I use this option a lot, when it comes to backups or to mantain source and destination files up to date.

Eventually you sync too much files and sometimes you just delete some files at source without removing them in destination. You know **rsync** does not remove any file at destination no matter if they do not exist at source, it just sync files/directories using blocks on files/directories that exist on both sides.

This is when **--delete** come handy. If you remove a file/s on soure and if you use this option, it will remove those files that doesn't exist in source at destination.

In our last article about [backups](https://dev.to/jmr_code_social/rsync-10-examples-in-11-days-day-04-1mkd) I used _~/Users_ folder as source on which I had a file called _file1.txt_ and we used it to sync and backup to our destination _~/Sync_ folder. I will create another file and will call it _file2.txt_ and remove our _file1.txt_ file.

```
iamgroot@laptop:~$ rm ~/Users/file1.txt && touch ~/Users/file2.txt && echo "Hello World" >> ~/Users/file2.txt && ls -F ~/Users/
file2.txt
```

Now, I will sync our source and destinations folders and check what we have in both folders

```
iamgroot@laptop:~$ ls -F ~/Users && ls -F ~/Sync
file2.txt
file1.txt  file2.txt
```

As you can see, in our source folder we only have _file2.txt_ and in our destination we still have _file1.txt_. Here is when we should use the **--delete** option. Using it, will remove files in destination missing at source

```
iamgroot@laptop:~$ rsync -vhr --delete ~/Users/ ~/Sync && ls -F ~/Users && ls -F ~/Sync
sending incremental file list
deleting file1.txt
file2.txt

sent 124 bytes  received 48 bytes  344.00 bytes/sec
total size is 12  speedup is 0.07
file2.txt
file2.txt
```

Now we only have _file2.txt_ in both folders. Nice!!

I do have to point out something. We do have few more options to play with:

<table>
<tr>
<td>--delete-before</td>
<td>receiver deletes before transfer, not during (this is the default)</td>
</tr>
<tr>
<td>--delete-during</td>
<td>receiver deletes during the transfer</td>
</tr>
<tr>
<td>--delete-delay</td>
<td>find deletions during, delete after</td>
</tr>
<tr>
<td>--delete-after</td>
<td>receiver deletes after transfer, not during</td>
</tr>
<tr>
<td>--delete-excluded</td>
<td>also delete excluded files from dest dirs</td>
</tr>
</table>

Another great option that we must include in this article is the **--remove-source-files** which removes from source synchronized files. Be aware that this _option_ don't remove directories. This _option_ is very useful when doing backups (tar, zip files), since backups are usually compressed files we barely want to keep those files at source after synching. Let's try it out, I will create a folder called ~/Users/backup, then I will create some files inside ~/Users/backup/ and at the end I will back them up using tar. After all this I will perform a sync and will check if it gets removed

```
iamgroot@laptop:~$ mkdir ~/Users/backup && touch ~/Users/backup/tobackup{1..5}.txt && tar -cvzf ~/Users/backup.tar.gz ~/Users/backup/ && ls -F ~/Users
tar: Removing leading `/' from member names
/home/iamgroot/Users/backup/
/home/iamgroot/Users/backup/tobackup3.txt
/home/iamgroot/Users/backup/tobackup4.txt
/home/iamgroot/Users/backup/tobackup5.txt
/home/iamgroot/Users/backup/tobackup2.txt
/home/iamgroot/Users/backup/tobackup1.txt
backup/  backup.tar.gz  file2.txt
```

Performing **rsync** with _--remove-source-files_ option

```
iamgroot@laptop:~$ rsync -vhr --remove-source-files ~/Users/backup.tar.gz ~/Sync && ls -F ~/Users
sending incremental file list
backup.tar.gz

sent 292 bytes  received 43 bytes  670.00 bytes/sec
total size is 196  speedup is 0.59
backup/  file2.txt
```

---

Ok, that'll be for today's example, thanks for reading!!!

Follow, ❤ or 🦄