Skip to content

Instantly share code, notes, and snippets.

@gfucci
Last active July 20, 2024 20:37
Show Gist options
  • Save gfucci/0c4b84fc25b40337d130ceb666a78621 to your computer and use it in GitHub Desktop.
Save gfucci/0c4b84fc25b40337d130ceb666a78621 to your computer and use it in GitHub Desktop.
Delete files recursively by extensions
<?php
// deletes all files with the chosen extensions. The script will run on all folders and subfolders starting from the folder where you ran the script (don't forget to make a backup first)
ini_set('memory_limit', -1);
$currentPath = getcwd();
$extensions = [
//put here extensions to delete
];
$count = 0;
function deleteFilesRecursively($currentPath, $extensions, $count)
{
global $count;
$folder = glob($currentPath . '/*');
foreach ($folder as $key => $folderFile) {
if (!is_dir($folderFile)) {
$path_parts = pathinfo($folderFile);
if (array_key_exists('extension', $path_parts)
&& in_array($path_parts['extension'], $extensions)
) {
$count++;
echo "arquivo: $folderFile" . PHP_EOL;
unlink($folderFile);
}
} else {
deleteFilesRecursively($folderFile, $extensions, $count);
}
}
}
deleteFilesRecursively($path, $extensions, $count);
echo "Deleted files: $count" . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment