7.x file.inc file_usage_list(stdClass $file)

Determines where a file is used.


$file: A file object.

Return value

A nested array with usage data. The first level is keyed by module name, the second by object type and the third by the object id. The value of the third level contains the usage count.

See also



Related topics

6 calls to file_usage_list()
FileDeleteTest::testInUse in modules/simpletest/tests/file.test
Tries deleting a file that is in use.
FilePrivateTestCase::testPrivateFile in modules/file/tests/file.test
Tests file access for file uploaded to a private node.
FileUsageTest::testGetUsage in modules/simpletest/tests/file.test
Tests file_usage_list().
file_field_delete_file in modules/file/file.field.inc
Decrements the usage count for a file and attempts to delete it.
file_managed_file_validate in modules/file/file.module
An #element_validate callback for the managed_file element.

... See full list


includes/file.inc, line 659
API for handling file uploads and server file management.


function file_usage_list(stdClass $file) {
  $result = db_select('file_usage', 'f')
    ->fields('f', array(
    ->condition('fid', $file->fid)
    ->condition('count', 0, '>')
  $references = array();
  foreach ($result as $usage) {
    $references[$usage->module][$usage->type][$usage->id] = $usage->count;
  return $references;


greggles’s picture

If you want to use this to find where files are used, here's a little script you can use in Drush:

// Save this, edit it and then...
// Run this with something like drush -v @sitename scr filename.php
// If a file is found and known to be in use, the use will be listed as an array of data.

// Edit the "card-1.xls" part.
$fid = db_query("SELECT fid FROM {file_managed} WHERE filename = 'filename.txt'")->fetchField();

if ($fid) {
  $file = file_load($fid);
else {
  echo "file not found";
brenk28’s picture

In D8: