3

I have around 600k images in one directory, and i need to delete almost half of them with PHP. In one array i have stored names of files that need to be deleted and when I try to loop through array and call unlink($filename) server crashes after ~1k iterations and I must reboot it (CentOS 7). PHP memory usage is 81MB, and thats because of large array, which i have used because it is faster for some comparation against files in directory, not important.

foreach($filenames as $filename){

  // Deletes file
  unlink($filename);
}

I tought, maybe i should try exec(), but it's very slow, and after some time it freezes too. I have also saw a discussion here about is it unlink() asynchronous, but I think it isn't.

I don't need rmdir() and deletion of directories, because all 600k files are in one directory, and relation between files and entities in application are stored in DB.

Is there any other alternative to delete files from PHP, or some advice how to make this thing work?

Community
  • 1
  • 1
Milos R
  • 71
  • 1
  • 5

1 Answers1

0

I have found one solution. Firt write list of all files into to_delete.txt file (one filename per line) using file_put_contents(). Then run xargs -a to_delete.txt -d'\n' rm to delete files (xarg adds as much arguments as it can to rm, it doesn't run rm command for each file).

Milos R
  • 71
  • 1
  • 5