1. drupal
    1. 8
    2. 7
7 – 8 file_usage_delete(stdClass $file, $module, $type = NULL, $id = NULL, $count = 1)

Removes a record to indicate that a module is no longer using a file.

The file_delete() function is typically called after removing a file usage to remove the record from the file_managed table and delete the file itself.


$file A file object.

$module The name of the module using the file.

$type (optional) The type of the object that contains the referenced file. May be omitted if all module references to a file are being deleted.

$id (optional) The unique, numeric ID of the object containing the referenced file. May be omitted if all module references to a file are being deleted.

$count (optional) The number of references to delete from the object. Defaults to 1. 0 may be specified to delete all references to the file within a specific object.

See also




Related topics

▾ 7 functions call file_usage_delete()

FileDeleteTest::testInUse in modules/simpletest/tests/file.test
Tries deleting a file that is in use.
FileUsageTest::testRemoveUsage in modules/simpletest/tests/file.test
Tests file_usage_delete().
file_field_delete_file in modules/file/
Decrements a file usage count and attempts to delete it.
image_example_style_form_submit in examples/image_example/
Form Builder; Display a form for uploading an image.
image_field_delete_field in modules/image/image.module
Implements hook_field_delete_field().
image_field_update_field in modules/image/image.module
Implements hook_field_update_field().
user_save in modules/user/user.module
Save changes to a user account or add a new user.


includes/, line 692

function file_usage_delete(stdClass $file, $module, $type = NULL, $id = NULL, $count = 1) {
  // Delete rows that have a exact or less value to prevent empty rows.
  $query = db_delete('file_usage')
    ->condition('module', $module)
    ->condition('fid', $file->fid);
  if ($type && $id) {
      ->condition('type', $type)
      ->condition('id', $id);
  if ($count) {
    $query->condition('count', $count, '<=');
  $result = $query->execute();

  // If the row has more than the specified count decrement it by that number.
  if (!$result && $count > 0) {
    $query = db_update('file_usage')
      ->condition('module', $module)
      ->condition('fid', $file->fid);
    if ($type && $id) {
        ->condition('type', $type)
        ->condition('id', $id);
    $query->expression('count', 'count - :count', array(':count' => $count));