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);
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";
}
}
}
?>
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?
source
share