What are the best practices for efficiently deleting multiple images with the same "partial image name" in PHP?

When deleting multiple images with the same partial image name in PHP, it is best to use a combination of functions like scandir() to get a list of files in the directory, preg_grep() to filter out files with the partial image name, and unlink() to delete the matching files efficiently.

$directory = 'path/to/images/';
$partialName = 'image_partial_name';

// Get list of files in directory
$files = scandir($directory);

// Filter out files with partial image name
$matchingFiles = preg_grep('/' . $partialName . '/', $files);

// Delete matching files
foreach ($matchingFiles as $file) {
    unlink($directory . $file);
}