$dir полный путь к директории
public function recursiveRemoveDir($dir):void
{
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileInfo) {
if (in_array($fileInfo->getBasename(), ['.', '..']) !== true) {
$todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileInfo->getPathName());
}
}
rmdir($dir);
}