Definition

file_delete($file, $force = FALSE)
drupal/includes/file.inc, line 701

Description

Delete a file and its database record.

If the $force parameter is not TRUE hook_file_references() will be called to determine if the file is being used by any modules. If the file is being used is the delete will be canceled.

@see file_unmanaged_delete() @see hook_file_references() @see hook_file_delete()

Parameters

$file A file object.

$force Boolean indicating that the file should be deleted even if hook_file_references() reports that the file is in use.

Return value

mixed TRUE for success, FALSE in the event of an error, or an array if the file is being used by another module. The array keys are the module's name and the values are the number of references.

Related topics

Namesort iconDescription
File interfaceCommon file handling functions.

Code

function file_delete($file, $force = FALSE) {
  $file = (object)$file;

  // If any module returns a value from the reference hook, the file will not
  // be deleted from Drupal, but file_delete will return a populated array that
  // tests as TRUE.
  if (!$force && ($references = module_invoke_all('file_references', $file))) {
    return $references;
  }

  // Let other modules clean up any references to the deleted file.
  module_invoke_all('file_delete', $file);

  // Make sure the file is deleted before removing its row from the
  // database, so UIs can still find the file in the database.
  if (file_unmanaged_delete($file->filepath)) {
    db_delete('files')->condition('fid', $file->fid)->execute();
    return TRUE;
  }
  return FALSE;
}