Quick Linux Tip:
If you’re trying to delete a very large number of files at one time (I deleted a directory with 485,000+ today), you will probably run into this error:
The problem is that when you type something like “rm -rf *”, the “*” is replaced with a list of every matching file, like “rm -rf file1 file2 file3 file4″ and so on. There is a reletively small buffer of memory allocated to storing this list of arguments and if it is filled up, the shell will not execute the program.
To get around this problem, a lot of people will use the find command to find every file and pass them one-by-one to the “rm” command like this:
My problem is that I needed to delete 500,000 files and it was taking way too long.
I stumbled upon a much faster way of deleting files – the “find” command has a “-delete” flag built right in! Here’s what I ended up using:
Using this method, I was deleting files at a rate of about 2000 files/second – much faster!
You can also show the filenames as you’re deleting them:
…or even show how many files will be deleted, then time how long it takes to delete them:
real 0m3.660s
user 0m0.036s
sys 0m0.552s
« Flex 2 DataGrid ItemRenderers are re-used! Warning!!! Bidirectional LocalConnections in Actionscript 3 (Flex 2 / Flex 3 / Flash CS3) »


Brilliant! Works like a CHAMP. Total lifesaver. We are using this to delete over 8 million cache files from a series of directories. Quick tip, using the first letter of a portion of the files seems to make it run faster. In our example, every filename is a random hexadecimal string. We know that the filenames will randomly begin with 0-9 or a-f. We are running 16 commands like the following:
find a* -type f -print -delete
Oh, and for those who may not know, the ‘-type f’ is for files and ‘-type d’ is for directories.
Hi Todd, I’m glad it worked for you!
Please note that in the form of the command you posted:
The a* will be expanded by the shell into a list of all the files that begin with a. It will also fail if there are too many files beginning with that letter/number. If this happens, you need to put the a* in quotes like this, to let the find command evaluate this shell expansion:
Oh perfect, I noticed that I was still getting the ‘Argument list too long.’ error. I will finish deleting the files using your suggestion.
Thanks again!!
I am getting a “No such file or directory” error with the single quotes around the file mask.
Oh, sorry, here’s the correct syntax:
Note: the ‘.’ is the path to look for the files in.
Steve Thank you very much worked for me.
Thanks for the article. Helped me much.
I just deleted 1.3 million files with this command: find . -type f -delete
It took about 10 seconds.
Total lifesaver! Watch where you use it though. This little nugget of information is a jewel that I will NEVER forget. Thanks!!
“I just deleted 1.3 million files”
“It took about 10 seconds.”
You must not be using XFS…
Perfect. Thanks for the advice. Was trying to work out how to delete Magento’s stupidly massive session storage (740k files). Will have to cron job that folder to stop that happening in the future.
Thanks, I now will delete so many files, that I can’t even count, cos server freezes ^^
I had too much file, much more than 500 000 and this is on production server which is high traffic. When I tried use “find” I’ve lost all my free memory after several minutes of it’s work. The only way I found is to write files removal with a simple script:
<?php
$dir = "/path/to/dir/with/files";
$dh = opendir( $dir);
$i = 0;
while (($file = readdir($dh)) !== false) {
$file = "$dir/$file";
if (is_file( $file)) {
unlink( $file);
if (!(++$i % 1000)) {
echo "$i files removed\n";
}
}
}
This do what I want and not use all my memory. If it will perform CPU or disk operations overloading it can be tuned to add sleep() or usleep() in removal cycle.
Best regards,
Mike
Thank you, this worked for me when nothing else seemed to!
Hi Thanks for the great article.
It is a good idea to combine the find . -type f -delete with ionice;
ionice -c 3 find . -type f delete with ionice;
This find example works but is not a good idea as it stones the server if it is already loaded with high i/o reads;
its better to use a bash loop
for i in $(echo *); do rm -f $i; done
best
Georgi
Thanks a ton, you saved my day
Dude! thanks a lot. this command helped me a lot to delete more than 0.7 GB from a directory
-print in not accepted on AIX box
I used the below command to remove all the files older than 30 days from a particular directory
find . -type f -mtime +30 -exec rm {} \;
I was trying with find . -type f -delete, but it wasn’t work for me
Instead I’ve used ls -f | xargs rm -rf
what advantage does it have? By default ls will sort the files, however the -f option avoid to do that, and when you have millions of files that boost the speed quite a lot.
xargs is a commands that allows you to apply actions on returned lists on the terminal.
`find . -name ‘*.del.*’ -type d -print -delete` -> find: cannot delete `/storage/00000/0000/000/91.del.5′: Directory not empty
It doesn’t work for directories.
Thanks for the tips. I had a runaway mailbox that had waaaay too many files in it. The rm command wasn’t removing them, but after a few seconds of running the find command with the delete flag you suggested, disk space started freeing up on the machine.
find . -type f -delete
Imho, is fast, but consumes too much HDD (high iowait might bring your site down)
find . -type f -exec rm -v {} \;
is a good alternative when you’re not in a hurry but prefer to have your site/routines up.
Hi, it doesn’t work for me ..
my command is something like this ..
find *.xls -type f -mtime +2 -print -delete
ps: my test system is fedora core 3 ..
find . -name ‘*.xls’ -type f -mtime +2 -print -delete
Deletes Excel files that are older than two days.
Great Thanks.
i wanted to delete all this file at one go
18608.xml,27866.xml,8003.xml,18610.xml,27869.xml,8009.xml,18613.xml,27880.xml,8010.xml,18616.xml,27888.xml,8015.xml,18618.xml,27903.xml,8023.xml,18635.xml,27910.xml,8040.xml,18636.xml,27911.xml,8045.xml,18637.xml,27915.xml,8063.xml,18648.xml,27921.xml,8066.xml,18655.xml,27930.xml,8068.xml,1866.xml,27931.xml,8070.xml,18660.xml,27933.xml,8074.xml,18664.xml,27943.xml,808.xml,18670.xml,27951.xml,8089.xml,18688.xml,27958.xml,8097.xml,18691.xml,27967.xml,8099.xml,18705.xml,27977.xml,8109.xml,18707.xml,27978.xml,8117.xml,18718.xml,27991.xml,8129.xml,18719.xml,27999.xml,8132.xml,18720.xml,28003.xml,8136.xml,18726.xml,28004.xml,8138.xml,18751.xml,28007.xml,8146.xml,18755.xml,28009.xml,8148.xml,18766.xml,28015.xml,815.xml,18768.xml,28017.xml,8158.xml,1877.xml,28023.xml,8164.xml,18770.xml,28033.xml,8168.xml,18774.xml,28042.xml,8176.xml,18778.xml,28052.xml,8192.xml,18780.xml,28057.xml,8201.xml,18788.xml,28058.xml,8209.xml,18798.xml,2807.xml,8218.xml,18813.xml,28072.xml,8225.xml,18815.xml,28082.xml,8226.xml,18824.xml,28085.xml,
8233.xml,18825.xml,28088.xml,8238.xml,18835.xml,28090.xml,8239.xml,18839.xml,28100.xml,8244.xml,18845.xml,28103.xml,8248.xml,18851.xml,28114.xml,8258.xml,18856.xml,28115.xml,8259.xml,18857.xml,28116.xml,826.xml,18858.xml,28119.xml,8262.xml,18871.xml,28140.xml,8268.xml,18873.xml,28144.xml,8278.xml,18874.xml,28148.xml,8291.xml,18884.xml,28151.xml,8292.xml,18895.xml,28155.xml,830.xml,1890.xml,28158.xml,8301.xml,18909.xml,28160.xml,8338.xml,18911.xml,28162.xml,8339.xml,18919.xml,2817.xml,8340.xml,18932.xml,28173.xml,8344.xml,18938.xml,28175.xml,8348.xml,18948.xml,28185.xml,8352.xml,18956.xml,28210.xml,8361.xml,18959.xml,28211.xml,8370.xml,1896.xml,28213.xml,8373.xml,18968.xml,28214.xml,8378.xml,18974.xml,28225.xml,8390.xml,18977.xml,28248.xml,8400.xml,18986.xml,28249.xml,8404.xml,18987.xml,28250.xml,8411.xml,18989.xml,28259.xml,8417.xml,18995.xml,28273.xml,842.xml,19003.xml,28277.xml,8427.xml,1901.xml,28292.xml,8436.xml,19014.xml,28293.xml,8438.xml,19016.xml,28294.xml,8439.xml,19021.xml,28298.xml,844.xml,19024.xml,28299.xml,
8443.xml,19027.xml,28306.xml,8449.xml,19028.xml,28327.xml,8453.xml,19030.xml,2833.xml,8467.xml,19031.xml,28331.xml,847.xml,19037.xml,28335.xml,8475.xml,19049.xml,28341.xml,8483.xml,1907.xml,28345.xml,8500.xml,19073.xml,28351.xml,8504.xml,19076.xml,28368.xml,8515.xml,19078.xml,28378.xml,8516.xml,19081.xml,28386.xml,8520.xml,19082.xml,28387.xml,8528.xml,19085.xml,28389.xml,8537.xml,19106.xml,2839.xml,8539.xml,19108.xml,28400.xml,854.xml,19113.xml,28402.xml,8552.xml,19114.xml,28403.xml,8554.xml,19117.xml,28414.xml,8562.xml,1912.xml,28420.xml,8575.xml,19121.xml,28421.xml,8580.xml,19122.xml,28424.xml,8583.xml,19125.xml,28425.xml,8596.xml,19137.xml,2843.xml,8602.xml,19149.xml,28431.xml,8603.xml,19150.xml,28434.xml,8604.xml,19152.xml,2844.xml,8608.xml,19158.xml,28443.xml,8619.xml,19161.xml,28467.xml,863.xml,19164.xml,28485.xml,8634.xml,19165.xml,28519.xml,8637.xml,19176.xml,28525.xml,864.xml,19183.xml,28533.xml,8659.xml,1920.xml,28548.xml,8663.xml,19211.xml,28554.xml,8670.xml,19212.xml,28555.xml,8672.xml,19213.xml,28557.xml,
8679.xml,19214.xml,28570.xml,8681.xml,19218.xml,28581.xml,8685.xml,19220.xml,28583.xml,8686.xml,19223.xml,28588.xml,8688.xml,19236.xml,28593.xml,8702.xml,19240.xml,28596.xml,8706.xml,19241.xml,28597.xml,8711.xml,19243.xml,28611.xml,8712.xml,19246.xml,28616.xml,8717.xml,19247.xml,28618.xml,8723.xml,19248.xml,28628.xml,8727.xml,19254.xml,28643.xml,8735.xml,19269.xml,28646.xml,8743.xml,19271.xml,28656.xml,8757.xml,19276.xml,28667.xml,8769.xml,
Pls help me how to delete all this files in one go.
at the same time i have few other .xml files to retain so let me know how to delete only the xml file name which is in numeric
Hi,
if you want to delete millions of files in linux, there are couple of methods that can be used. The problem discussed here, of deleting files with rm is not the problem of rm command, its the limitation of bash argument list. Find with delete option is much better than exec options, because exec will spawn a processes.
The below articles explains the whole thing in detail,
http://www.slashroot.in/which-is-the-fastest-method-to-delete-files-in-linux