Is there a way to get all the managed files used by a particular entity, i.e. a node or other module item?

Looking in the file_usage table there is an id column and this is what I want to retrieve fid's by.

Seems like something that there would be a function for, but I haven't found it yet...

1 Answer 1


I don't think there is such a function. However it's not hard to create your own. Here's an example:

function load_files_by_type($type, $id) {
  // Select all used files by type and id.
  $query = db_select('file_usage', 'fu')
    ->fields('fu', array('fid'))
    ->condition('fu.type', $type)
    ->condition('fu.id', $id);

  // Load all file ids in an array.
  $fids = $query->execute()->fetchCol();

  // Return the file objects.
  return file_load_multiple($fids);

You can call it like this: load_files_by_type('node', 64);

Of course you can alter the query to your liking, you could even add a module check.

  • Thanks Patrick. I'll accept your answer if no one else chimes in with a specific Drupal function based approach. Seems weird that there isn't - I'm not sure I understand what the file_usage table is for if not this kind of thing... Commented Jan 13, 2016 at 9:24
  • Naturally, I'm curious myself if such a function exists. Commented Jan 13, 2016 at 9:27
  • Still there is no such function in drupal 8 also? If so, it's weird. If there is any such function added, can someone post here.
    – siddiq
    Commented Oct 4, 2017 at 3:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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