File usage functions

Last updated on
4 February 2018

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

One of the things to think about when using the Drupal 7 File API is the new file_usage_* functions. These provide a way for modules to register that they are using a file so file_delete() won't delete the file if it is still required. There are 3 file_usage_* functions:

  • file_usage_list(): Lists the usage of a file given the file object as an argument.
  • file_usage_delete(): Removes a record of a file being used.
  • file_usage_add(): Records that a module uses a file, what object it's used in, what type that object is and the module that is responsible.

When a file is saved in Drupal 7 it will not automatically register it with file_usage_add(). This will have to be done separately. To remove the file you have to use file_usage_delete() to remove the listed usage of the file. But remember that this might break other modules, so the best approach is not to remove the file as long as it's used by another module.

Help improve this page

Page status: No known problems

You can: