You are here

function file_unmanaged_delete_recursive in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/includes/file.inc \file_unmanaged_delete_recursive()

Deletes all files and directories in the specified filepath recursively.

If the specified path is a directory then the function will call itself recursively to process the contents. Once the contents have been removed the directory will also be removed.

If the specified path is a file then it will be passed to file_unmanaged_delete().

Note that this only deletes visible files with write permission.

Parameters

$path: A string containing either an URI or a file or directory path.

$callback: (optional) Callback function to run on each file prior to deleting it and on each directory prior to traversing it. For example, can be used to modify permissions.

Return value

TRUE for success or if path does not exist, FALSE in the event of an error.

See also

file_unmanaged_delete()

Related topics

15 calls to file_unmanaged_delete_recursive()
BrowserTestBase::cleanupEnvironment in core/modules/simpletest/src/BrowserTestBase.php
Clean up the Simpletest environment.
hook_uninstall in core/lib/Drupal/Core/Extension/module.api.php
Remove any information that the module sets.
ImageStyle::flush in core/modules/image/src/Entity/ImageStyle.php
Flushes cached media for this style.
image_uninstall in core/modules/image/image.install
Implements hook_uninstall().
RetrieveFileTest::testFileRetrieving in core/modules/system/src/Tests/System/RetrieveFileTest.php
Invokes system_retrieve_file() in several scenarios.

... See full list

File

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

Code

function file_unmanaged_delete_recursive($path, $callback = NULL) {
  if (isset($callback)) {
    call_user_func($callback, $path);
  }
  if (is_dir($path)) {
    $dir = dir($path);
    while (($entry = $dir
      ->read()) !== FALSE) {
      if ($entry == '.' || $entry == '..') {
        continue;
      }
      $entry_path = $path . '/' . $entry;
      file_unmanaged_delete_recursive($entry_path, $callback);
    }
    $dir
      ->close();
    return drupal_rmdir($path);
  }
  return file_unmanaged_delete($path);
}