5

I am deleting orphaned image files in Drupal 7, but I have to first cross reference them to other tables that might be using the images. Given that I don't upload any images via the IMCE editor interface and mainly do uploads via the image module, would it be safe to say i can cross reference the table file_managed and image_dimensions to file_usage and delete what's not there?

Right now I'm just wondering how file_usage is populated. Is it populated every time I upload any file type?

7

The code used by Drupal to delete temporary files is the following one. (It runs during cron tasks.)

  // Remove temporary files that are older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
  // Use separate placeholders for the status to avoid a bug in some versions
  // of PHP. See http://drupal.org/node/352956.
  $result = db_query('SELECT fid FROM {file_managed} WHERE status <> :permanent AND timestamp < :timestamp', array(
    ':permanent' => FILE_STATUS_PERMANENT,
    ':timestamp' => REQUEST_TIME - DRUPAL_MAXIMUM_TEMP_FILE_AGE,
  ));
  foreach ($result as $row) {
    if ($file = file_load($row->fid)) {
      $references = file_usage_list($file);
      if (empty($references)) {
        if (!file_delete($file)) {
          watchdog('file system', 'Could not delete temporary file "%path" during garbage collection', array('%path' => $file->uri), WATCHDOG_ERROR);
        }
      }
      else {
        watchdog('file system', 'Did not delete temporary file "%path" during garbage collection, because it is in use by the following modules: %modules.', array('%path' => $file->uri, '%modules' => implode(', ', array_keys($references))), WATCHDOG_INFO);
      }
    }
  }

file_usage_list() is the function that queries the "file_usage" table.
The function that adds a row in that table is file_usage_add(); looking at the functions/hooks that calls it, I conclude that the "file_usage" table is used for managed files. Since you are not deleting the files with a row in the "file_managed" table, I would say you can avoid checking the "file_usage" table.

4

The canonical way t do it, is to add usage mark when you use a picture, and remove it when you are no longer doing it. It means image with 3 references and one original upload will have 4 usage marks. When something reference it once more, it'll gets bumped to 5. If something unreference, one usage mark goes away. And if you only uploaded file with something like #managed_file file is NOT marked as used untill form is saved, and even then only if function handling submit decides it should be.

When invoking file_delete(), only if usage count 0, Drupal will delete a file - both file itself from hdd and all its metadata from tables. No tests on your side are needed, you can simply loop it over all files you want, or call it after each usage delete (that's usual practice).

The idea is - you should never interact with that table directly, only via file_usage_add(), file_usage_delete() and file_usage_list(). And you should never delete actual file or it's database record. Well, unless it's needed immediately due to content security issue or something like that. But otherwise, Drupal takes care.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.