function FileSystem::delete

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::delete()
  2. 8.9.x core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::delete()
  3. 11.x core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::delete()

Deletes a file without database changes or hook invocations.

This function should be used when the file to be deleted does not have an entry recorded in the files table.

Parameters

string $path: A string containing a file path or (streamwrapper) URI.

Overrides FileSystemInterface::delete

1 call to FileSystem::delete()
FileSystem::deleteRecursive in core/lib/Drupal/Core/File/FileSystem.php
Deletes all files and directories in the specified filepath recursively.

File

core/lib/Drupal/Core/File/FileSystem.php, line 316

Class

FileSystem
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\Core\File

Code

public function delete($path) {
  if (is_file($path)) {
    if (!$this->unlink($path)) {
      throw new FileException("Failed to unlink file '{$path}'.");
    }
    return TRUE;
  }
  if (is_dir($path)) {
    throw new NotRegularFileException("Cannot delete '{$path}' because it is a directory. Use deleteRecursive() instead.");
  }
  // Return TRUE for non-existent file as the current state is the intended
  // result.
  if (!file_exists($path)) {
    return TRUE;
  }
  // We cannot handle anything other than files and directories.
  // Throw an exception for everything else (sockets, symbolic links, etc).
  throw new NotRegularFileException("The file '{$path}' is not of a recognized type so it was not deleted.");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.