8.2.x file.inc file_delete($fid)
8.0.x file.inc file_delete($fid)
8.1.x file.inc file_delete($fid)
8.3.x file.inc file_delete($fid)
4.6.x file.inc file_delete($path)
4.7.x file.inc file_delete($path)
5.x file.inc file_delete($path)
6.x file.inc file_delete($path)
7.x file.inc file_delete(stdClass $file, $force = FALSE)

Deletes a file and its database record.

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

Parameters

$file: A file object.

$force: Boolean indicating that the file should be deleted even if the file is reported as in use by the file_usage table.

Return value

mixed TRUE for success, FALSE in the event of an error, or an array if the file is being used by any modules.

See also

file_unmanaged_delete()

file_usage_list()

file_usage_delete()

hook_file_delete()

Related topics

9 calls to file_delete()
EntityCrudHookTestCase::testFileHooks in modules/simpletest/tests/entity_crud_hook_test.test
Tests hook invocations for CRUD operations on files.
FileDeleteTest::testInUse in modules/simpletest/tests/file.test
Tries deleting a file that is in use.
FileDeleteTest::testUnused in modules/simpletest/tests/file.test
Tries deleting a normal file (as opposed to a directory, symlink, etc).
FileDownloadTest::checkUrl in modules/simpletest/tests/file.test
Download a file from the URL generated by file_create_url().
file_field_delete_file in modules/file/file.field.inc
Decrements the usage count for a file and attempts to delete it.

... See full list

File

includes/file.inc, line 1274
API for handling file uploads and server file management.

Code

function file_delete(stdClass $file, $force = FALSE) {
  if (!file_valid_uri($file->uri)) {
    if (($realpath = drupal_realpath($file->uri)) !== FALSE) {
      watchdog('file', 'File %file (%realpath) could not be deleted because it is not a valid URI. This may be caused by improper use of file_delete() or a missing stream wrapper.', array('%file' => $file->uri, '%realpath' => $realpath));
    }
    else {
      watchdog('file', 'File %file could not be deleted because it is not a valid URI. This may be caused by improper use of file_delete() or a missing stream wrapper.', array('%file' => $file->uri));
    }
    drupal_set_message(t('The specified file %file could not be deleted, because it is not a valid URI. More information is available in the system log.', array('%file' => $file->uri)), 'error');
    return FALSE;
  }

  // If any module still has a usage entry in the file_usage table, the file
  // will not be deleted, but file_delete() will return a populated array
  // that tests as TRUE.
  if (!$force && ($references = file_usage_list($file))) {
    return $references;
  }

  // Let other modules clean up any references to the deleted file.
  module_invoke_all('file_delete', $file);
  module_invoke_all('entity_delete', $file, '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_managed')->condition('fid', $file->fid)->execute();
    db_delete('file_usage')->condition('fid', $file->fid)->execute();
    entity_get_controller('file')->resetCache();
    return TRUE;
  }
  return FALSE;
}

Comments

pdcarto’s picture

I used the following hook_update function to clean out all unused files (change the function name as appropriate for your module):

/**
 * Delete unused files
 */
function mymodule_update_7000(&$sandbox) {
  $results = db_query("SELECT fm.fid FROM {file_managed} fm LEFT JOIN {file_usage} fu ON fu.fid = fm.fid WHERE fu.count IS NULL");
  $count = 0;
  foreach($results as $result) {
    $file = file_load($result->fid);
    file_delete($file);
    $count++;
  }
  print $count . " files deleted\n";
}
phponwebsites’s picture

How to delete a file from public folder?
I've litte confusion.
my code is below

$file ='public://not_imported_longanswer.csv';
$path = $base_url . '/sites/default/files/not_imported_longanswer.csv';
if (file_exists($file)) {
    file_delete($path);
}

How to make stdClass object for file?

kingandy’s picture

If you know you're dealing with a file that Drupal is managing (it's been uploaded or attached to an entity), you can find the file ID from the file_managed table using the URI:

$path = 'public://not_imported_longanswer.csv';
$fid = db_query("SELECT fid FROM {file_managed} WHERE uri = :path", array(':path' => $path))->fetchField();
$file = file_load($fid);
file_delete($file);

(It may be sufficient to do a SELECT * and use the row out of the file_managed table as the argument for the file_delete() call - using fetch() instead of fetchField() - but it's probably safer to file_load in case some other module is modifying file objects.)

If you know your file has not gone anywhere near the Drupal's file API, you can remove the file from your file system using the file_unmanaged_delete() function:

$path = 'public://not_imported_longanswer.csv';
file_unmanaged_delete($path);

And if you're not sure, you can combine the two into a logical check:

$path = 'public://not_imported_longanswer.csv';
$fid = db_query("SELECT fid FROM {file_managed} WHERE uri = :path", array(':path' => $path))->fetchField();
if ($fid) {
  $file = file_load($fid);
  file_delete($file);
}
else {
  file_unmanaged_delete($path);
}