Deleting tons of files in Linux using rsync

If You have a problem with deleting tons of files in linux (when for example You’ve came across this problem: /bin/rm: Argument list too long.) You can try find, perl scripts and so on (you can find some more info here), but if described there ways to do this are not successful You can try my way to cope with this problem using rsync.

First of all You need to tune Your system a little bit (put those commands as root in console):

sysctl -w vm.dirty_ratio=10
sysctl -w vm.dirty_background_ratio=5

(why we are doing this)

Now we are ready to delete those files:

Create empty directory:

mkdir /empty

Now use rsync to copy empty directory over Your directory (attention it will delete all the contents of destination directory):

rsync -a --delete /empty/ /todelete/

It can take a lot of time, and maybe You want to use ionice to be nice for other processes and services that are using disk during this operation. It is also good to set this command on screen.

After above command will end its job just do:

rmdir /todelete

Of course it can also be I/O consuming operation so You may also want to use ionice command.

The above way helped me to delete almost 100 millions of files in one directory where find, perl and other ways were not successful.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.