Delete all files matching patter using PHP glob()

When you have the need to do a little house cleaning on your storage system programmatically you can use PHP’s glob() and array_map() functions to make easy work of this.

In this example, we are going to find all text files in “some/dir/” and unlink (aka: delete) them. The PHP glob() function gets us our list of files and then the array_map() function passes each index value to the callback function, in this case, the “unlink” system command.


array_map(callback, array)



The result of this

Author: Richard Soares

Director of Software Development & Senior Technical Project Manager