Delete huge amount of files in a directory

Hard_DiskI had this peculiar problem – one of my virtual hosts had a custom tmp directory where sessions were stored and not deleted for a long time. When I found out, the number of files in there already posed a big problem because commands like ls or rm * failed due to a limit to * expansion wildcard. My next thought was to use find to get rid of them:

find . -type f -delete

which worked just fine, the only problem being that it was so resource-hungry that the load average would soar skywards – not an option on a live system.

The solution was to use ionice, which can tell the process to proceed only when there is free IO time available (the option -c 3).

find . -type f -exec ionice -c 3 rm {} \;

The action took much longer, of course, but the load never went higher than just a little bit above 1.