Delete a huge number of files

I have 8 million files in my / tmp and I need to delete them. A rather important application also runs on this server, and I cannot overload it.

I am using a small php script:

<?php
$dir = "/tmp";
$dh = opendir( $dir);
$i = 0;
while (($file = readdir($dh)) !== false) {
  $file = "$dir/$file";
  if (is_file( $file) && (preg_match("/open/", $file))) {
    unlink( $file);
    #echo $file;
    if (!(++$i % 10000)) {
      echo "$i files removed\n";
    }
  }
}
?>

but it makes my application inaccessible even if: $ ionice -c 3 php./tmp_files_killer.php $ nice -n 20 php./tmp_files_killer.php

I changed my script so that it doesn't read / tmp dir all the time:

$ ls -1 /tmp > tmp_files_list.txt

<?php
$file = "tmp_files_list.txt"; 
$infile = fopen($file, "r"); 

while ( !feof( $infile ) ) { 
  $line = rtrim( fgets( $infile ), "\n\r" ); 
  if ($line != null){ 
    $file = "$dir/$line";
    unlink( $file);
    if (!(++$i % 10000)) {
      echo "$i files removed\n";
    }
#    echo $line + "\n";
  } 
} 
?>

but running this script also slows down my application. The process does not load the CPU and I have enough memory.

Guys, how to delete these files?

+5
source share
6 answers

You can get the script to work in "pieces" and then sleep between each fragment.

sleep() , , 30 . , , .

cron, , .

+4

linux-, ,

find /tmp -type f -exec rm -v {} \;

, , cronjob ,

+3

-: .

- tmpfs, ext2 ext3, , , (USB-?), .

mv /tmp , /tmp, mke2fs , /tmp, .

tmp , - mv /tmp /new-tmp, , , mount /tmp, , .

+1

, . ... exec() rm /path/to/clean/*, .

Not very clean, but at least it worked well for me.

0
source

Do i need to do a cleanup using php script?

If not, take a look at this article ... you will get some ideas

0
source

This is probably the faster and more standard way to delete all files in / tmp:

find /tmp -type f -exec rm {} +

With Gnu detection, this can be a little faster:

find /tmp -type f -delete

If / tmp is in your own file system, just disable mkfs as well. If it is tmpfs, just reboot.

0
source

All Articles