We are using the services module to provide a JSON feed for our internal portal to pick up our articles. The page view that provides all files for each node uses the File Usage: Content relationship. We just noticed that even files that have been deleted from the node are still being listed.

The query that views shows (when I take that same view and set as a block)

SELECT file_managed.uri AS file_managed_uri, file_managed.fid AS fid, file_managed.filemime AS file_managed_filemime, users_file_managed.mail AS users_file_managed_mail, 'file' AS field_data_field_caption_file_entity_type, 'file' AS field_data_field_file_image_alt_text_file_entity_type, 'file' AS field_data_field_file_image_title_text_file_entity_type FROM {file_managed} file_managed LEFT JOIN {file_usage} file_usage ON file_managed.fid = file_usage.fid INNER JOIN {node} node_file_usage ON file_usage.id = node_file_usage.nid AND file_usage.type = 'node' LEFT JOIN {users} users_file_managed ON file_managed.uid = users_file_managed.uid WHERE (( (node_file_usage.nid = '') ))

The nid is provided as a parameter.

How can I get it to not include files that were removed from the node?

  • perhaps file_usage.count != '0' as filter Commented Jun 12, 2015 at 5:08
  • That sounds logical, but it didn't work. I tried it both with and without the relationship.
    – shelane
    Commented Jun 12, 2015 at 15:12
  • When I display usecount, even the deleted image says 1, the non-deleted image says 24.
    – shelane
    Commented Jun 12, 2015 at 15:20

1 Answer 1


You should use file_usage_delete to remove file usage records to nodes that files was having relations so you stop that problem for future deleting files on nodes.

And you still have to fix it for the existing nodes I suggest two ways

First : write a script to loop on file_usage records and check if the not currently used in the node (via node load and check fid exist) if so delete that record (if u have small number of rows in that table)

Second : Implement hook_views_pre_render and do the same check.

The first is preferred however it may take long time but it will stop the problem reason.

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.