Used to clear a directory of matching files.

@param string $path The path to search.
@param int $now The current timestamp
@param int $threshold Any file not modified after this value will be deleted.
@return void
438479
protected function _clearDirectory($path, $now, $threshold)
    {
        if (!is_dir($path)) {
            return;
        }
        $prefixLength = strlen($this->_config['prefix']);

        $dir = dir($path);
        while (($entry = $dir->read()) !== false) {
            if (substr($entry, 0, $prefixLength) !== $this->_config['prefix']) {
                continue;
            }

            try {
                $file = new SplFileObject($path . $entry, 'r');
            } catch (Exception $e) {
                continue;
            }

            if ($threshold) {
                $mtime = $file->getMTime();
                if ($mtime > $threshold) {
                    continue;
                }

                $expires = (int)$file->current();
                if ($expires > $now) {
                    continue;
                }
            }
            if ($file->isFile()) {
                $filePath = $file->getRealPath();
                $file = null;

                //@codingStandardsIgnoreStart
                @unlink($filePath);
                //@codingStandardsIgnoreEnd
            }
        }

        $dir->close();
    }
713751
public static function recursive($path)
    {
        if (is_dir($path)) {
            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::CHILD_FIRST);
            foreach ($iterator as $file) {
                self::_deleteFile($file);
            }
            rmdir($path);
        }
    }
1305855
public function emptyFolders($path = null) {
		if ($path) {
			$path = realpath($path);
		}
		if (!$path) {
			$path = ROOT . DS;
		}

		$this->out('Clearing empty folders in ' . $path);

		$this->_clearEmpty($path);
	}
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]);
    }
142260
private static function rmdir($path) {
        foreach (glob($path . '/*') as $file) {
            if (is_dir($file)) {
                self::rmdir($file);
            } else {
                unlink($file);
            }
        }

        // Remove the remaining directory
        rmdir($path);
    }
108465
final public function delete($path)
    {

        //If we have permission to remove this file
        if ($this->isWritable($path)) {
            if (!@unlink($path)) //This is highly unreliable as unlink returns a warning not a bool
            {
                return false;
            }
        } else {
            if (!@unlink($path)) //This is highly unreliable as unlink returns a warning not a bool
            {
                return false;
            }
        }

        return true;
    }
1048239
protected function _clearPath($path, $targets)
    {
        if (!file_exists($path)) {
            $this->verbose("Not clearing '$path' it does not exist.");
            return;
        }

        $dir = new DirectoryIterator($path);
        foreach ($dir as $file) {
            $name = $base = $file->getFilename();
            if (in_array($name, array('.', '..'))) {
                continue;
            }
            // timestampped files.
            if (preg_match('/^(.*)\.v\d+(\.[a-z]+)$/', $name, $matches)) {
                $base = $matches[1] . $matches[2];
            }
            if (in_array($base, $targets)) {
                $this->verbose(' - Deleting ' . $path . $name, '.');
                unlink($path . $name);
                continue;
            }
        }
    }
292376
private static void deleteIfExists(Path path) throws IOException {
		FileSystem fs = path.getFileSystem();
		if (fs.exists(path)) {
			fs.delete(path, true);
		}
	}
1346336
protected function _clearPath($path, $themes, $targets)
    {
        if (!file_exists($path)) {
            return;
        }

        $dir = new DirectoryIterator($path);
        foreach ($dir as $file) {
            $name = $base = $file->getFilename();
            if (in_array($name, ['.', '..'])) {
                continue;
            }
            // timestamped files.
            if (preg_match('/^(.*)\.v\d+(\.[a-z]+)$/', $name, $matches)) {
                $base = $matches[1] . $matches[2];
            }
            // themed files
            foreach ($themes as $theme) {
                if (strpos($base, $theme) === 0 && strpos($base, '-') !== false) {
                    list($themePrefix, $base) = explode('-', $base);
                }
            }
            if (in_array($base, $targets)) {
                $this->_io->verbose(' - Deleting ' . $path . $name);
                unlink($path . $name);
                continue;
            }
        }
    }
390482
public function resetPath(string $path)
    {
        foreach ($this->cache as &$cache) {
            unset($cache[$path], $cache);
        }
    }
193222
private function shouldBeRemoved($path)
    {
        $path = $this->normalizePath($path);

        // Always remove "newup.keep" files.
        if (Str::is('*newup.keep', $path)) {
            return true;
        }

        foreach ($this->automaticallyRemovedPaths as $removedPath) {
            if (Str::is($this->normalizePath($removedPath), $path)) {
                return true;
            }
        }

        return false;
    }
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());
		}
	}
840258
@SuppressWarnings("ResultOfMethodCallIgnored")
    static private void deleteDirectory(File path) {
        if (path.exists()) {
            //noinspection ConstantConditions
            for (File f : path.listFiles()) {
                if (f.isDirectory()) {
                    deleteDirectory(f);
                }
                f.delete();
            }
            path.delete();
        }
    }
1116315
public static function removeDir($path)
    {
        if (is_file($path)) {
            unlink($path);
        } else {
            foreach (scandir($path) as $dir) {
                if ($dir === '.' || $dir === '..') {
                    continue;
                }

                static::removeDir($path.'/'.$dir);
            }

            rmdir($path);
        }
    }
1303338
protected function _rrmdir( $path )
    {
        $i = new DirectoryIterator( $path );
        foreach( $i as $f )
        {
            if( $f->isFile() )
            {
                $this->warn( 'Permanently removed file ' . str_replace( base_path(), null, $f->getRealPath() ) );
                unlink( $f->getRealPath() );
            }
            else if( !$f->isDot() && $f->isDir() )
            {
                $this->_rrmdir( $f->getRealPath() );
            }
        }
        $this->warn( 'Permanently removed directory' . str_replace( base_path(), null, $path ) );
        rmdir( $path );
    }
927400
private function removePath(string $path): void
    {
        if (false !== strpos($path, '*')) {
            $paths = glob($path);
            foreach ($paths as $path) {
                $this->removePath($path);
            }
        } else {
            if (is_dir($path)) {
                $iterator = new \RecursiveDirectoryIterator($path);
                $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
                /** @var \SplFileInfo $file */
                foreach ($iterator as $file) {
                    if ('.' === $file->getBasename() || '..' === $file->getBasename()) {
                        continue;
                    }
                    if ($file->isLink() || $file->isFile()) {
                        unlink($file->getPathname());
                    } else {
                        rmdir($file->getPathname());
                    }
                }
                rmdir($path);
            } else {
                if (is_file($path)) {
                    unlink($path);
                }
            }
        }
    }
236747
public function remove($path)
    {
        StructureHelper::removeByPath($this->data, $path, $this->separator);

        return $this;
    }