file_delete

Versions
4.6 – 6
file_delete($path)
7
file_delete(stdClass $file, $force = FALSE)

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 also

file_unmanaged_delete()

@see hook_file_references()

See also

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

▾ 9 functions call file_delete()

file_field_delete_file in modules/file/file.field.inc
Check that File controls a file before attempting to delete it.
file_managed_file_process in modules/file/file.module
Process function to expand the managed_file element type.
file_move in includes/file.inc
Move a file to a new location and update the file's database entry.
hook_node_revision_delete in modules/node/node.api.php
Respond to deletion of a node revision.
system_cron in modules/system/system.module
Implement hook_cron().
upload_node_delete in modules/upload/upload.module
Implement hook_node_delete().
upload_node_revision_delete in modules/upload/upload.module
Implement hook_node_revision_delete().
upload_save in modules/upload/upload.module
user_save in modules/user/user.module
Save changes to a user account or add a new user.

Code

includes/file.inc, line 929

<?php
function file_delete(stdClass $file, $force = FALSE) {
  // 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->uri)) {
    db_delete('file')->condition('fid', $file->fid)->execute();
    return TRUE;
  }
  return FALSE;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.