Delete backup files

@param string $lang_folder_path
@param int $days
@param bool|false $dryRun
@param string $ext

@return bool
197866
public function deleteBackupFiles($lang_folder_path, $days = 0, $dryRun = false, $ext = 'php')
    {
        if ($days < 0) {
            return false;
        }

        try {
            $dir_lang = $this->getLangPath($lang_folder_path);
        } catch (Exception $e) {
            switch ($e->getCode()) {
                //@codeCoverageIgnoreStart
                case self::NO_LANG_FOLDER_FOUND_IN_THESE_PATHS:
                    $this->messageBag->writeError("No lang folder found in these paths:");
                    foreach ($e->getParameter() as $path) {
                        $this->messageBag->writeError("- ".$path);
                    }
                    break;
                //@codeCoverageIgnoreEnd

                case self::NO_LANG_FOLDER_FOUND_IN_YOUR_CUSTOM_PATH:
                    $this->messageBag->writeError('No lang folder found in your custom path: "'.$e->getParameter().'"');
                    break;
            }

            return false;
        }

        $return = true;

        foreach ($this->getBackupFiles($dir_lang) as $file) {
            $fileDate = $this->getBackupFileDate($file, $ext);

            // @codeCoverageIgnoreStart
            // Cannot happen because of glob but safer
            if (is_null($fileDate)) {
                $this->messageBag->writeError('Unable to detect date in file '.$file);
                $return = false;

                continue;
            }
            // @codeCoverageIgnoreEnd

            if ($this->isDateOlderThanDays($fileDate, $days)) {
                if ($dryRun === true) {
                    $deleted = true;
                } else {
                    $deleted = unlink($file);
                }

                if ($deleted === true) {
                    $this->messageBag->writeInfo('Deleting file '.$file);
                } // @codeCoverageIgnoreStart
                else {
                    $this->messageBag->writeError('Unable to delete file '.$file);

                    $return = false;
                }
            } // @codeCoverageIgnoreEnd
            else {
                $this->messageBag->writeInfo('Skip file '.$file.' (not older than '.$days.'day'.Tools::getPlural($days).')');
            }
        }

        return $return;
    }
12958
static function delete_generated_files( $local_file ) {
		if ( URLHelper::is_absolute($local_file) ) {
			$local_file = URLHelper::url_to_file_system($local_file);
		}
		$info = pathinfo($local_file);
		$dir = $info['dirname'];
		$ext = $info['extension'];
		$filename = $info['filename'];
		self::process_delete_generated_files($filename, $ext, $dir, '-[0-9999999]*', '-[0-9]*x[0-9]*-c-[a-z]*.');
		self::process_delete_generated_files($filename, $ext, $dir, '-lbox-[0-9999999]*', '-lbox-[0-9]*x[0-9]*-[a-zA-Z0-9]*.');
		self::process_delete_generated_files($filename, 'jpg', $dir, '-tojpg.*');
		self::process_delete_generated_files($filename, 'jpg', $dir, '-tojpg-[0-9999999]*');
	}
634814
public function delTree($dir)
    {
        if (! file_exists($dir)) {
            return false;
        }

        $files = array_diff(scandir($dir), ['.', '..']);

        $this->deleteAllFiles($dir, $files);

        return rmdir($dir);
    }
757469
static function cleanDirectory($dirPath, $keepStructure = false)
    {
        $scanResult = static::scanDirectory($dirPath);
        if (!$scanResult) return false;

        try {
            foreach ($scanResult['files'] as $file) unlink($file);
            if (!$keepStructure) {
                krsort($scanResult['dirs']);
                foreach ($scanResult['dirs'] as $dir) rmdir($dir);
            }
            return true;
        } catch (\Throwable $throwable) {
            return false;
        }
    }
1081626
public function cleanLog($path = '', $dayToDel = 3)
    {
        try {
            $getDir             = $this->directoryScanner($path, $this->scanInclude, $this->scanExclude);
            $result             = [];
            $result['scanPath'] = $path;
            foreach ($getDir as $fileName) {
                $SplFileInfo = new \SplFileInfo($fileName);
                $filename    = $SplFileInfo->getPathname();
                $format      = 'YmdHis';
                // Lấy thời gian xác định xóa fileName
                $dateTime   = new \DateTime("-" . $dayToDel . " days");
                $deleteTime = $dateTime->format($format);
                // Lấy modifyTime của file
                $getfileTime = filemtime($filename);
                $fileTime    = date($format, $getfileTime);
                if ($fileTime < $deleteTime) {
                    $this->chmod($filename, 0777);
                    $this->remove($filename);
                    $result['listFile'][] .= "Delete file: " . $filename;
                }
            }

            return $result;
        }
        catch (\Exception $e) {
            $message = 'Error File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Code: ' . $e->getCode() . ' - Message: ' . $e->getMessage();
            if (function_exists('log_message')) {
                log_message('error', $message);
            }

            return NULL;
        }
    }
139573
protected function deleteZip($backup_id)
    {
        $backup = $this->get($backup_id);

        if (empty($backup['path'])) {
            return false;
        }

        $file = gplcart_file_absolute($backup['path']);
        return file_exists($file) && unlink($file);
    }
865991
public function syncProjectPaths($paths, $dryRun = false) {
		foreach($paths as $path) {
			if(!DNProject::get()->filter('Name', $path)->count()) {
				$this->message($path);
				if(!$dryRun) {
					DNProject::create_from_path($path)->write();
				}
			}
		}
	}
323909
public function useDailyFiles($path, $days = 0, $level = 'debug')
	{
		$level = $this->parseLevel($level);

		$this->monolog->pushHandler(new RotatingFileHandler($path, $days, $level));
	}
252821
public static function remdir($path, $leave_folder = false)
    {
        if (!is_dir($path)) {
            return false;
        }

        // Always have trailing slash
        $slash = substr($path, -1);
        if ($slash != '/' && $slash != '\\') {
            $path .= '/';
        }

        // Remove all inner folders
        foreach (array_diff(scandir($path), ['.', '..']) as $v) {
            if (is_dir($path . $v)) {
                self::remdir($path . $v . '/', $leave_folder);
            } elseif (!is_writable($path . $v) || !unlink($path . $v)) {
                return false;
            }
        }

        // Skip latest folder if required
        if ((!$leave_folder) && !rmdir($path)) {
            return false;
        }

        return true;
    }
960225
private function deleteFileOrFolder(Filesystem $filesystem, string $path): bool
    {
        if ($filesystem->has($path)) {
            try {
                $metadata = $filesystem->getMetadata($path);

                if ($metadata['type'] === 'dir') {
                    $filesystem->deleteDir($path);
                }

                if ($metadata['type'] === 'file') {
                    $filesystem->delete($path);
                }
            } catch (Exception $ex) {
                return false;
            }
        }

        return true;
    }
929427
public static function deleteAttachment($routesConfigFile, $resource, $objectId, $lang = null)
    {
        Attachment::deleteAttachment([
            'lang_id_016'       => $lang,
            'resource_id_016'   => $resource,
            'object_id_016'     => $objectId
        ]);

        if(isset($lang))
        {
            if(!empty($objectId) &&  !empty($lang))
            {
                // delete all attachments from this object
                $response = File::deleteDirectory(public_path() . config($routesConfigFile . '.attachmentFolder') . '/' . $objectId. '/' . $lang);
            }
            else
            {
                throw new InvalidArgumentException('Object Id, is not defined to delete attachment files');
            }

        }
        else
        {
            if(!empty($objectId))
            {
                // delete all attachments from this object
                $response = File::deleteDirectory(public_path() . config($routesConfigFile . '.attachmentFolder') . '/' . $objectId);
            }
            else
            {
                throw new InvalidArgumentException('Object Id, is not defined to delete attachment files');
            }
        }

        return $response;
    }
1061142
public function rmdir(string $path, int $options): bool
    {
        $path = $this->resolvePath(vfsStream::path($path));

        /** @var vfsStreamDirectory|null $child */
        $child = $this->getContentOfType($path, vfsStreamContent::TYPE_DIR);
        if (null === $child) {
            return false;
        }

        // can only remove empty directories
        if (count($child->getChildren()) > 0) {
            return false;
        }

        if (self::$root->getName() === $path) {
            // delete root? very brave. :)
            self::$root = null;
            clearstatcache();
            return true;
        }

        $names = $this->splitPath($path);

        /** @var vfsStreamDirectory $dir */
        $dir = $this->getContentOfType($names['dirname'], vfsStreamContent::TYPE_DIR);
        if ($dir->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) {
            return false;
        }

        clearstatcache();
        return $dir->removeChild($child->getName());
    }
284885
static public function clear_backup_dir($backupid, \core\progress\base $progress = null) {
        $backupiddir = make_backup_temp_directory($backupid, false);
        if (!self::delete_dir_contents($backupiddir, '', $progress)) {
            throw new backup_helper_exception('cannot_empty_backup_temp_dir');
        }
        return true;
    }
960527
private function cleanup(OutputInterface $output, NodeInterface $node, $rootPath, $dryRun)
    {
        foreach ($node->getNodes() as $childNode) {
            $this->cleanup($output, $childNode, $rootPath, $dryRun);
        }

        $path = ltrim(str_replace($rootPath, '', $node->getPath()), '/');

        if (!$node->getPropertyValueWithDefault('sulu:history', false)) {
            $output->writeln(
                '<info>Processing aborted: </info>/' .
                $path . ' <comment>(no history url)</comment>'
            );

            return;
        }

        if (false === $dryRun) {
            $node->remove();
        }
        $output->writeln('<info>Processing: </info>/' . $path);
    }
749442
public static function isValidDirectory( $dir_lang , $lang )
	{
		if ( ! in_array( $lang , array( "." , ".." ) ) )
		{
			if ( is_dir( $dir_lang . DIRECTORY_SEPARATOR . $lang ) )
			{
				return true;
			}
		}

		return false;
	}
966880
function delete_file($filepath)
	{
		if ( ! $this->_is_conn())
		{
			return false;
		}

		$result = @ftp_delete($this->_conn_id, $filepath);

		if ($result === false)
		{
			if ($this->_debug == true)
			{
				throw new \FtpFileAccessException('Unable to delete');
			}
			return false;
		}

		return true;
	}
1239487
public function delete($remote_path, $recursive = false)
    {
        $remote_path = $this->_getAbsoluteRemotePath($remote_path);
        if (true === @is_dir($remote_path)) {
            if (true === $recursive) {
                foreach ($this->ls($remote_path) as $file) {
                    if ('.' !== $file && '..' !== $file) {
                        $this->delete($remote_path.DIRECTORY_SEPARATOR.$file, $recursive);
                    }
                }
            }

            if (false === @rmdir($remote_path)) {
                return $this->_trigger_error(sprintf('Unable to delete directory %s', $remote_path));
            }
        } else {
            if (false === @unlink($remote_path)) {
                return $this->_trigger_error(sprintf('Unable to delete file %s', $remote_path));
            }
        }

        return true;
    }