You are here

How to cleanly delete a Drupal file with drush

This is a simple trick which (unless my googlefu simply failed me) I didn't find described anywhere when I had a quick look:

$ drush ev '$file = file_load(21749); var_dump(file_delete($file, TRUE));'
bool(true)

This means all the appropriate hooks are called in file_delete so the Drupal API gods should smile on you, and you should get to see the TRUE/FALSE result reflecting success or otherwise. Note that we're passing $force=TRUE "indicating that the file should be deleted even if the file is reported as in use by the file_usage table." So be careful.

To delete multiple files you could use file_load_multiple but there's not a corresponding file_delete_multiple function, so you'd have to loop over the array of file objects.

That's all there is to this one.