Skip to content

Instantly share code, notes, and snippets.

@kyleskrinak
Last active August 29, 2015 14:08
Show Gist options
  • Save kyleskrinak/93cf1e8bbb57f656d362 to your computer and use it in GitHub Desktop.
Save kyleskrinak/93cf1e8bbb57f656d362 to your computer and use it in GitHub Desktop.
Removes all files that have no associated usage. Run as drush php-script cleanup.drush
#!/usr/bin/env drush
<?php
//db_query to find all files not attached to a node:
$result = db_query("SELECT fid FROM {file_managed} m WHERE NOT EXISTS (SELECT * FROM {file_usage} u WHERE m.fid = u.fid)");
echo $result->rowCount();
// how much did I delete?
$foundCounter = 0;
//Delete file & database entry
for ($i = 1; $i <= $result->rowCount(); $i++) {
$record = $result->fetchObject();
$file = file_load($record->fid);
if ($file != NULL && strpos($file->uri, 'public://news') !== false) {
$foundCounter++;
echo $file->uri . "\r\n";
// file_delete($file);
}
}
echo "Deleted " . $foundCounter . " files.\r\n";
@kyleskrinak
Copy link
Author

Added:

  1. a filter to only remove unused managed files in the news directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment