Recursive delete an empty directory.

@param string $dir
@return bool
347088
protected function removeChildDirecotries(string $dir = null)
    {
        if (empty($dir)) {
            $dir = $this->cacheDir;
        }

        $success = true;
        $objects = new GlobIterator($dir);

        foreach ($objects as $object) {
            if (!is_dir("$dir/$object") && is_link("$dir/$object")) {
                $success = $this->recursiveRemove("$dir/$object") && rmdir($dir) && $success;
            }
        }

        return $success;
    }
982684
public static function deleteDirectory($dir) {
		if (is_dir($dir))
			$dirHandle = opendir($dir);
		
		if (!$dirHandle)
			return false;
		
		while ($file = readdir($dirHandle)) {
			if ($file != "." && $file != "..") {
				if (!is_dir($dir."/".$file))
					unlink($dir."/".$file);
				else
					self::deleteDirectory($dir.'/'.$file);          
			}
		}
		
		closedir($dirHandle);
		rmdir($dir);
		return true;
	}
275533
public static boolean deleteDir(File dir) {        
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (String file : children) {
                boolean success = deleteDir(new File(dir, file));
                if (!success) {
                    return false;
                }
            }
        }

        // The directory is now empty so delete it
        return dir.delete();
    }
946359
function recursiveRemoveDir($dir)
    {
        if (is_link($dir)) {
            @unlink($dir);
        } else if (is_dir($dir)) {
            $files = array_diff(@scandir($dir), array('.', '..'));
            foreach ($files as $file) {
                (is_dir("$dir/$file")) ? $this->recursiveRemoveDir("$dir/$file") : @unlink("$dir/$file");
            }
        }
        return @rmdir($dir);
    }
296806
protected function deleteDirectory($dir)
    {
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );

        foreach ($files as $fileInfo) {
            $todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink');

            $todo($fileInfo->getRealPath());
        }

        rmdir($dir);
    }
1125075
final public static function isDirEmpty($dir)
    {
        if (!is_readable($dir)) {
            return (null);
        }
        $handle = opendir($dir);
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != "..") {
                return (false);
            }
        }
        return (true);
    }
405265
protected static function cleanDir($dir) {
		
		if (isset($dir[0])) {
			$dir = trim($dir);
			$dir = ($dir[0] == DIRECTORY_SEPARATOR) ? ltrim($dir, DIRECTORY_SEPARATOR) : $dir;
			$dir = ($dir[strlen($dir)-1] == DIRECTORY_SEPARATOR) ? rtrim($dir, DIRECTORY_SEPARATOR) : $dir;
		}
		
		return $dir;
		
	}
464190
public static function removeDirectory($directory)
    {
        $inner = new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS);
        $outer = new \RecursiveIteratorIterator($inner, \RecursiveIteratorIterator::SELF_FIRST);

        // remove the files first
        foreach ($outer as $file) {
            if ($file->isFile()) {
                unlink($file);
            }
        }

        // remove the sub-directories next
        $files = iterator_to_array($outer);
        foreach (array_reverse($files) as $file) {
            /** @var \SplFileInfo $file */
            if ($file->isDir()) {
                rmdir($file);
            }
        }

        // finally the directory itself
        rmdir($directory);
    }
582924
protected function rmdir($dir) {
        $iterator = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dir),
            RecursiveIteratorIterator::CHILD_FIRST
        );

        foreach ($iterator as $file) {
            $name = $file->getPathname();

            if (substr($name, -1) === '.') {
                continue;
            }

            if ($file->isDir()) {
                // Remove dir
                rmdir($name);
            } else {
                // Remove file
                unlink($name);
            }
        }

        // Remove the directory itself
        rmdir($dir);
    }
1220044
public static function deleteFiles($dir) {
		$dir = self::normalizePath($dir);
		if (\is_dir($dir)) {
			$files = \scandir($dir);
			foreach ($files as $file) {
				$fullPath = self::normalizePath(self::combinePath($dir, $file));
				if ($file != "." && $file != "..") {
					if (\is_dir($fullPath))
						self::deleteFiles($fullPath);
					else
						@\unlink($fullPath);
				}
			}
			@\rmdir($dir);
		}
		return true;
    }
35070
public function rmdir($directory = null)
    {
        if (\ftp_rmdir($this->_stream, $directory)) {
            return true;
        }

        $this->error = 'Failed to remove directory "'.$directory.'"';

        return false;
    }
1276093
public static void deleteChilsDirsIfEmpty(File dir){
		File childs[]=getChildDirs(dir);
		
		for (File file:childs){
			deleteChilsDirsIfEmpty(file);
			if (!file.delete()) {
				log.info("No se ha podido borrar el directorio " + file.getAbsolutePath());
			}
		}
		
	}
1344571
protected function deleteFromDir(
        /*# string */ $dir,
        /*# int */ $maxlife = 0,
        /*# bool */ $removeDir = false
    )/*# : bool */ {
        $now = time();
        if (is_dir($dir)) {
            $files = scandir($dir);
            foreach ($files as $file) {
                if ($file == "." || $file == "..") {
                    continue;
                }
                $sub = $dir . DIRECTORY_SEPARATOR . $file;
                $res = true;
                if (is_dir($sub)) {
                    $res = $this->deleteFromDir($sub, $maxlife, true);
                } else {
                    if (!$maxlife || $now - filemtime($sub) > $maxlife) {
                        $res = unlink($sub);
                    }
                }
                if ($res === false) {
                    return $this->falseAndSetError(
                        Message::get(Message::CACHE_FAIL_DELETE, $sub),
                        Message::CACHE_FAIL_DELETE
                    );
                }
            }
            if ($removeDir && ! @rmdir($dir)) {
                return $this->falseAndSetError(
                    Message::get(Message::CACHE_FAIL_DELETE, $dir),
                    Message::CACHE_FAIL_DELETE
                );
            }
        }
        return true;
    }
723812
public function mkdir($dir)
    {
        return ($this->isConnected() === true) ? ftp_mkdir($this->getConnection(), $dir) : false;
    }
1259869
static function removeDir($dir,$depthLock=0) {
		if ($depthLock > 15) {
			throw new \RuntimeException("Recursion too deep at $dir");
		}
		if (!file_exists($dir)) {
			return true;
		}
		if (!is_dir($dir)) {
			throw new \InvalidArgumentException("$dir is not directory.");
		}

		$content=glob($dir."/*");
		if ($content) {
			foreach($content as $sub) {
				if ($sub=="." or $sub=="..") continue;
				if (is_dir($sub)) {
					self::removeDir($sub,$depthLock+1);
				} else {
					if (is_writable($sub)) {
						unlink($sub);
					} else {
						throw new FileAccessException("Could not delete file $sub");
					}
				}
			}
		}
		$ok=rmdir($dir);
		if (!$ok) {
			throw new FileAccessException("Could not remove dir $dir");
		}

		return true;
	}
1005904
public static function truncate_directory ($dir)
    {
        if (substr($dir, strlen($dir)-1, 1) != '/') {
            $dir .= '/';
        }

        if (!$fileList = scandir ($dir)) {
            return false;
        }

        foreach ($fileList as $fileInList) {
            if (is_dir($dir . $fileInList) && $fileInList != '.' && $fileInList != '..') {
                // Some more directory. Recurse!
                if (!self::rmrf($dir . $fileInList . '/')) {
                    // If false, Error message was already given by rmrf.
                    return false;
                }
            } elseif (is_file($dir . $fileInList)) {
                if (!unlink ($dir . $fileInList)) {
                    return false;
                }
            }
        }

        return true;
    }
861706
private function removeFilesFromDir($dir)
    {
        if ($handle = opendir($dir)) {
            while (false !== ($entry = readdir($handle))) {
                if ($entry == "." || $entry == "..") {
                    continue;
                }

                if (!unlink($dir.DIRECTORY_SEPARATOR.$entry)) {
                    $this->putWarning("Can not remove: ".$dir.DIRECTORY_SEPARATOR.$entry);
                }
            }
            closedir($handle);
        }
    }