Empty an entire directory

@param  boolean $remove
@param  string  $path
@return void
182223
public function emptyDir($remove = false, $path = null)
    {
        if (null === $path) {
            $path = $this->path;
        }
        // Get a directory handle.
        if (!$dh = @opendir($path)) {
            return;
        }

        // Recursively dig through the directory, deleting files where applicable.
        while (false !== ($obj = readdir($dh))) {
            if ($obj == '.' || $obj == '..') {
                continue;
            }
            if (!@unlink($path . DIRECTORY_SEPARATOR . $obj)) {
                $this->emptyDir(true, $path . DIRECTORY_SEPARATOR . $obj);
            }
        }

        // Close the directory handle.
        closedir($dh);

        // If the delete flag was passed, remove the top level directory.
        if ($remove) {
            @rmdir($path);
        }
    }
887766
static public function emptyDirectory($path, $ignoreSystemFiles = false)
    {
        $success = true;
        $handle = opendir($path);
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != "..") {
                if ($entry[0] === '.' && $ignoreSystemFiles === true) {
                    continue;
                }

                $success = self::remove($path . DIRECTORY_SEPARATOR . $entry) ? $success : false;
            }
        }
        closedir($handle);
        return $success;
    }
1159193
public function clean($folder, $removeDirectory = false)
    {
        if ( ! is_dir($folder)) {
            throw new \Exception(('Not a folder.'));
        }

        array_map('unlink', glob($folder . DIRECTORY_SEPARATOR . '*'));
        if ($removeDirectory && file_exists($folder)) {
            rmdir($folder);
        }
    }
108724
static function remove($path, $onlyFiles=false) {
		if (!file_exists($path)) {
			return true;
		}
		if (is_dir($path)) {
			if ($onlyFiles) throw new FileException("$path is a directory!");
			return self::removeDir($path);
		}
		else {
			$ok=unlink($path);
			if (!$ok) throw new FileAccessException("Could not delete file $path");
		}
		return true;
	}
618401
public function remove($force = false)
    {
        $isEmpty = $this->isEmpty();
        if ($force && !$isEmpty) {
            $it = new \RecursiveDirectoryIterator($this->dir, \RecursiveDirectoryIterator::SKIP_DOTS);
            $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
            foreach($files as $file) {
                if ($file->isDir()){
                    \rmdir($file->getRealPath());
                } else {
                    \unlink($file->getRealPath());
                }
            }

            \rmdir($this->dir);
            $this->removed = true;

            return;
        }

        if ($isEmpty) {
            \rmdir($this->dir);
            $this->removed = true;
        }
    }
1347583
protected function purge($path)
	{
		$directory = pathinfo($path, PATHINFO_DIRNAME);
		while ($directory != $this->uploadPath)
		{
			if (count(glob($directory .'/*')))
			{
				break;
			}

			rmdir($directory);
			$directory = dirname($directory);
		}
	}
259817
public function removePath($path)
	{
		$path = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
		$this->finder->removePath($path);

		if ($this->environment)
		{
			$this->finder->removePath($path.$this->environment);
		}
	}
1265125
public static function remove(string $dir = '', bool $emptyOnly = false): bool
    {
        $ret = true;
        $dir = static::normalizePath($dir);

        if (static::exists($dir)) {
            if ($handle = @\opendir($dir)) {
                while (false !== ($item = @\readdir($handle))) {
                    if ($item !== '.' && $item !== '..') {
                        $item = $dir.DIRECTORY_SEPARATOR.$item;
                        \is_dir($item) ? static::remove($item) : @\unlink($item);
                    }
                }
                @\closedir($handle);
            }
            if ($emptyOnly === false) {
                @\rmdir($dir);
            }
            return true;
        }
        return $ret;
    }
205537
public function recursiveRemoveDirectory($path)
    {
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );
        foreach ($files as $file) {
            if ($file->isDir()) {
                self::removeDirectory($file->getRealPath());
            } else {
                self::unlink($file->getRealPath());
            }
        }
        self::removeDirectory($path);
    }
63498
function cleanDir( path ) {
    var files = FS.readdirSync( path );
    try {
        files.forEach( function ( file ) {
            var fullpath = Path.join( path, file );
            if ( !FS.existsSync( fullpath ) ) return;
            var stat = FS.statSync( fullpath );
            try {
                if ( stat.isDirectory() ) PathUtils.rmdir( fullpath );
                else FS.unlinkSync( fullpath );
            } catch ( ex ) {
                console.error( "Unable to delete `" + fullpath + "`!" );
                console.error( ex );
            }
        } );
    } catch ( ex ) {
        fatal( "Fatal error in function `files`!\n" + JSON.stringify( ex, null, '  ' ) );
    }
}
719409
public static function clear($path = null) {
		if (empty($path)) {
			self::$_items = array();
			return;
		}

		if (Hash::check(self::$_items, $path)) {
			self::$_items = Hash::insert(self::$_items, $path, array());
		}
	}
675734
protected function _clean($dir)
    {
        foreach (scandir($dir, SCANDIR_SORT_ASCENDING) as $item) {
            if ($item === '.' || $item === '..') {
                continue;
            }

            $path = $dir . '/' . $item;
            if (is_file($path)) {
                if (time() > filemtime($path)) {
                    @unlink($path);
                }
            } else {
                $this->_clean($path);
            }
        }
    }
1120892
public function remove($path)
    {
        $path = explode($this->separator, $path);
        $last = array_pop($path);

        $item = &$this->item;

        foreach ($path as $key) {
            if (array_key_exists($key, $item) && is_array($item[$key])) {
                $item = &$item[$key];
                continue;
            }
            return false;
        }
        
        if (array_key_exists($last, $item)) {
            unset($item[$last]);
            return true;
        }

        return false;
    }
1019216
private function remove_dir($path){
        if( is_dir($path) ){
            foreach( scandir($path) as $file ){
                if( $file != '.' && $file != '..' ){
                    if( is_dir("{$path}/{$file}") ){
                        $this->remove_dir("{$path}/{$file}");
                    }else{
                        @unlink("{$path}/{$file}");
                    }
                }
            }
            @rmdir($path);
        }
    }
828011
public static function RemoveFolder($Path) {
      if (!file_exists($Path))
         return;

      if (is_file($Path)) {
         unlink($Path);
         return;
      }

      $Path = rtrim($Path, '/').'/';

      // Get all of the files in the directory.
      if ($dh = opendir($Path)) {
         while (($File = readdir($dh)) !== false) {
            if (trim($File, '.') == '')
               continue;

            $SubPath = $Path.$File;

            if (is_dir($SubPath))
               self::RemoveFolder($SubPath);
            else
               unlink($SubPath);
         }
         closedir($dh);
      }
      rmdir($Path);
   }
857573
public static function deleteFolder($path) 
	{
		if(file_exists($path)) {
			$fh = dir($path);
			//print_r($fh);
			while (false !== ($files = $fh->read())) {
				/*
				echo $fh->path.'/'.$files."\n";
				chmod($fh->path.'/'.$files, 0755);
				if(@unlink($fh->path.'/'.$files))
					echo '1'."\n";
				else
					echo '0'."\n";
				*/
				@unlink($fh->path.'/'.$files);
			}
			$fh->close();
			@rmdir($path);

			return true;

		} else 
			return false;
	}
691391
public static function remove(array &$array, string $path): void
    {
        $paths = explode('.', $path);
        $lastKey = array_pop($paths);
        $temp = &$array;
        while ($key = array_shift($paths)) {
            if (isset($temp[$key])) {
                $temp = &$temp[$key];
            } else {
                return;
            }
        }
        unset($temp[$lastKey]);
    }