Is there any rsync command that can remove some remote directory from rsync share completely? I mean delete not only directory content but the directory itself. And without rsyncing the entire parent directory since it can be very large.

  • Exactly what are you trying to achieve? Can you show some more detail about your setup and rsync command?
    – shodanshok
    Apr 16, 2015 at 8:44
  • Assume that we have a remote server that has one share. There are some directories in the share. Let x be one of them. I want a single rsync command that can delete x recursively and completely like rm -rf.
    – gkuzmin
    Apr 16, 2015 at 9:19

3 Answers 3


Answering my own question... In fact it is possible to remove directory from the remote server completely (including directory itself). Check the example below:

rsync -r --delete --include 'x/***' --exclude '*' local_empty_directory/ rsync://some_server/some_share/x_parent_directory

The key is to clean the parent directory for x (x_parent_directory in example) using local empty directory local_empty_directory while excluding everything by using --exclude '*' and including only the directory we want to delete (x in example) and its content by --include 'x/***'.


Following man of rsync you can use --exclude


This option is a simplified form of the --filter option that defaults to an exclude rule and does not allow the full rule-pars-ing syntax of normal filter rules.


This option is related to the --exclude option, but it specifies a FILE that contains exclude patterns (one per line). Blank lines in the file and lines starting with ';' or '#' are ignored. If FILE is -, the list will be read from standard input.

  • I do not understand your solution completely. It looks like I should run rsync --list-only to get a list of directories and then use exclude pattern that excludes everything except the directory I want to delete. If I am correct then this solution contains 2 subsequent rsync calls. I want to use only one rsync invocation to avoid concurrent access issues as much as possible.
    – gkuzmin
    Apr 16, 2015 at 9:23
  • Ooops, my fault, I understood remove as not using it in rsync.
    – alphamikevictor
    Apr 16, 2015 at 9:29
  • Actually this is OK since my question is badly formed. In fact I tried the 2 rsync invocation solution and it works fine except concurrent access issues and it looks like there is no better approach.
    – gkuzmin
    Apr 16, 2015 at 9:34

You can not remove remote directories with rsync. The best you can do is to remove all files inside a remote directory, using the "--remove-source-files" option. From the man page:

This 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.

Alternatively, you could simply issue a rm command via SSH, for example:

ssh root@remote_ip -c "rm -rf /somedir"

  • Looks like I did it, please review my answer.
    – gkuzmin
    Apr 16, 2015 at 11:16
  • I'm glad to see that you solved your problem. However, if you have SSH access to the remote server, why do not use the ssh -c method described above? It seems a more logical and direct solution...
    – shodanshok
    Apr 16, 2015 at 13:01
  • Because I do not have ssh, only rsync
    – gkuzmin
    Apr 16, 2015 at 16:48
  • 1
    Fair enough. :)
    – shodanshok
    Apr 16, 2015 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .