Last active
August 29, 2015 14:08
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added: