best syntax to delete very large number of sessions files in directory

regular delete syntax runs out of memory

what is best syntax for deleting millions of session files from sessions directory (without deleting the directory)?

rm -rf   ?

mkdir empty_dir 
rsync -a --delete empty_dir/    yourdirectory/ ?


Alex Glaros

I'd do it using xargs. First, run this to make sure you've got the right directory etc:

ls /tmp/foo/* | xargs -n1 echo rm

...where /tmp/foo is the directory. That should print out something like this:

rm /tmp/foo/1
rm /tmp/foo/2
rm /tmp/foo/3
rm /tmp/foo/4
rm /tmp/foo/5
rm /tmp/foo/6
rm /tmp/foo/7
rm /tmp/foo/8
rm /tmp/foo/9
rm /tmp/foo/10

....where 1, 2, 3 and so on are the first ten files. Double-check that the filenames look sane, then run the command again, but this time remove the echo and the head so that it runs for real:

ls /tmp/foo/* | xargs -n1 rm