Skip to content

Instantly share code, notes, and snippets.

@AronNovak
Created February 21, 2022 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AronNovak/3a695c6b950a0ef8fa35175ef9b37878 to your computer and use it in GitHub Desktop.
Save AronNovak/3a695c6b950a0ef8fa35175ef9b37878 to your computer and use it in GitHub Desktop.
Drupal 9 - unused / unmanaged unsupervised file cleanup script.
<?php
/**
* @file
* File cleanup script, to be executed after the migration.
*
* Invocation:
* ddev drush scr web/modules/custom/server_migrate/file_cleanup.php .
*/
$start = time();
// Let's gather all the unused files.
// Unused here means that it is not referenced in a file reference field
// and not used by a media. This covers all the use-cases.
// Beware that `file_usage` might not be bulletproof.
$file_ids = \Drupal::database()->query("
SELECT
fm.fid
FROM
{file_managed} fm
LEFT JOIN
{file_usage} fu
ON fm.fid=fu.fid
WHERE
fu.fid IS NULL")->fetchCol();
$file_storage = \Drupal::entityTypeManager()->getStorage('file');
$file_storage->delete($file_storage->loadMultiple($file_ids));
print "Deleted " . count($file_ids) . " unused files\n";
/**
* Filesystem service.
*
* @var \Drupal\Core\File\FileSystemInterface $fs_service
*/
$fs_service = \Drupal::service('file_system');
$file_uris = \Drupal::database()->query("
SELECT
uri
FROM
{file_managed}
")->fetchCol();
$used_paths = [];
foreach ($file_uris as $file_uri) {
$used_paths[] = $fs_service->realpath($file_uri);
}
$directory = new \RecursiveDirectoryIterator($fs_service->realpath("public://"));
$iterator = new \RecursiveIteratorIterator($directory);
$delete_count = 0;
/**
* File info.
*
* @var \SplFileInfo $info
*/
foreach ($iterator as $info) {
$absolute_path = $info->getRealPath();
if (in_array($absolute_path, $used_paths)) {
continue;
}
if (is_dir($absolute_path)) {
continue;
}
if (unlink($absolute_path)) {
$delete_count++;
}
}
print "Deleted $delete_count unmanaged files\n";
print "Execution time: " . (time() - $start) . " seconds\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment