Delete an existing directory including all child objects.

@param aDir
The directory to be deleted. May not be <code>null</code>.
@return A non-<code>null</code> error code.
496291
@Nonnull
  public static FileIOError deleteDirRecursive (@Nonnull final Path aDir)
  {
    ValueEnforcer.notNull (aDir, "Directory");

    final Path aRealDir = _getUnifiedPath (aDir);

    // Non-existing directory?
    if (!aRealDir.toFile ().isDirectory ())
      return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError (EFileIOOperation.DELETE_DIR_RECURSIVE, aRealDir);

    if (isExceptionOnDeleteRoot ())
    {
      // Check that we're not deleting the complete hard drive...
      if (aRealDir.getParent () == null || aRealDir.getNameCount () == 0)
        throw new IllegalArgumentException ("Aren't we deleting the full drive: '" + aRealDir + "'");
    }

    // Is the parent directory writable?
    final Path aParentDir = aRealDir.getParent ();
    if (aParentDir != null && !Files.isWritable (aParentDir))
      return EFileIOErrorCode.SOURCE_PARENT_NOT_WRITABLE.getAsIOError (EFileIOOperation.DELETE_DIR_RECURSIVE, aRealDir);

    // iterate directory
    for (final Path aChild : PathHelper.getDirectoryContent (aRealDir))
    {
      final File aChildFile = aChild.toFile ();

      // is it a file or a directory or ...
      if (aChildFile.isDirectory ())
      {
        // Ignore "." and ".." directory
        if (FilenameHelper.isSystemInternalDirectory (aChild))
          continue;

        // recursive call
        final FileIOError eCode = deleteDirRecursive (aChild);
        if (eCode.isFailure ())
          return eCode;
      }
      else
        if (aChildFile.isFile ())
        {
          // delete file
          final FileIOError eCode = deleteFile (aChild);
          if (eCode.isFailure ())
            return eCode;
        }
        else
        {
          // Neither directory no file - don't know how to handle
          return EFileIOErrorCode.OBJECT_CANNOT_BE_HANDLED.getAsIOError (EFileIOOperation.DELETE_DIR_RECURSIVE, aChild);
        }
    }

    // Now this directory should be empty -> delete as if empty
    return deleteDir (aRealDir);
  }
679872
@Nonnull
  public static FileIOError createDir (@Nonnull final Path aDir)
  {
    ValueEnforcer.notNull (aDir, "Directory");

    final Path aRealDir = _getUnifiedPath (aDir);
    return FileOperations.createDir (aRealDir.toFile ());

    // // Does the directory already exist?
    // if (Files.exists (aRealDir))
    // return EFileIOErrorCode.TARGET_ALREADY_EXISTS.getAsIOError
    // (EFileIOOperation.CREATE_DIR, aRealDir);
    //
    // // Is the parent directory writable?
    // final Path aParentDir = aRealDir.getParent ();
    // if (aParentDir != null && Files.exists (aParentDir) && !Files.isWritable
    // (aParentDir))
    // return EFileIOErrorCode.SOURCE_PARENT_NOT_WRITABLE.getAsIOError
    // (EFileIOOperation.CREATE_DIR, aRealDir);
    //
    // return _perform (EFileIOOperation.CREATE_DIR, Files::createDirectory,
    // aRealDir);
  }
1236411
public static void deleteFileTree(File directory) throws IOException {
        if (directory.exists()) {
            Files.walkFileTree(directory.toPath(), DeletingFileVisitor.INSTANCE);
        }
    }
931076
public static function delete(string $sDir): void
    {
        if (is_dir($sDir)) {
            $oFiles = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator(
                    $sDir,
                    RecursiveDirectoryIterator::SKIP_DOTS
                ),
                RecursiveIteratorIterator::CHILD_FIRST
            );

            foreach ($oFiles as $oFile) {
                $sfunction = $oFile->isDir() ? 'rmdir' : 'unlink';
                $sfunction($oFile->getRealPath());
            }

            rmdir($sDir);
        }
    }
30363
public void cleanDirectory( final File directory )
        throws IOException
    {
        if ( !directory.exists() )
        {
            final String message = directory + " does not exist";
            throw new IllegalArgumentException( message );
        }

        if ( !directory.isDirectory() )
        {
            final String message = directory + " is not a directory";
            throw new IllegalArgumentException( message );
        }

        Delete deleteTask = new Delete();
        deleteTask.setProject(getProject());
		deleteTask.setDir(directory);
		deleteTask.execute();
		
		if(!directory.exists()){
			directory.mkdirs();
		}
    }
977797
public static void recursiveDelete(Path directory) throws IOException   {

        try {
            Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws java.io.IOException {
                    try {
                        Files.delete(file);
                    } catch (NoSuchFileException ignore) {}
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, java.io.IOException exc) throws java.io.IOException {
                    try{
                        Files.delete(dir);
                    }catch (NoSuchFileException ignore){}
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (NoSuchFileException ignore) {}

    }
464933
function (startDir) {
            var dirFileArray, i, fileName, filePath, stat;

            if (file.exists(startDir)) {
                dirFileArray = fs.readdirSync(startDir);
                for (i = 0; i < dirFileArray.length; i++) {
                    fileName = dirFileArray[i];
                    filePath = path.join(startDir, fileName);
                    stat = fs.statSync(filePath);
                    if (stat.isDirectory()) {
                        file.deleteEmptyDirs(filePath);
                    }
                }

                //If directory is now empty, remove it.
                if (fs.readdirSync(startDir).length ===  0) {
                    file.deleteFile(startDir);
                }
            }
        }
609415
@Nonnull
  public static FileIOError deleteFile (@Nonnull final File aFile)
  {
    ValueEnforcer.notNull (aFile, "File");

    if (!FileHelper.existsFile (aFile))
      return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError (EFileIOOperation.DELETE_FILE, aFile);

    // Is the parent directory writable?
    final File aParentDir = aFile.getParentFile ();
    if (aParentDir != null && !aParentDir.canWrite ())
      return EFileIOErrorCode.SOURCE_PARENT_NOT_WRITABLE.getAsIOError (EFileIOOperation.DELETE_FILE, aFile);

    try
    {
      Files.delete (aFile.toPath ());
      return EFileIOErrorCode.NO_ERROR.getAsIOError (EFileIOOperation.DELETE_FILE, aFile);
    }
    catch (final NoSuchFileException ex)
    {
      return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError (EFileIOOperation.DELETE_FILE, aFile);
    }
    catch (final DirectoryNotEmptyException ex)
    {
      return EFileIOErrorCode.OPERATION_FAILED.getAsIOError (EFileIOOperation.DELETE_FILE, aFile);
    }
    catch (final IOException ex)
    {
      return EFileIOErrorCode.IO_ERROR.getAsIOError (EFileIOOperation.DELETE_FILE, aFile);
    }
    catch (final SecurityException ex)
    {
      return EFileIOErrorCode.getSecurityAsIOError (EFileIOOperation.DELETE_FILE, ex);
    }
  }
1210981
private void deleteExecutorDirs(String[] dirs) {
    for (String localDir : dirs) {
      try {
        JavaUtils.deleteRecursively(new File(localDir));
        logger.debug("Successfully cleaned up directory: {}", localDir);
      } catch (Exception e) {
        logger.error("Failed to delete directory: " + localDir, e);
      }
    }
  }
663798
public static void recursivelyDeleteDirectoryOnExit(final File directory) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        try {
          recursivelyDeleteDirectory(directory);
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    });
  }
490638
public static void recursivelyDelete(File f) throws IOException {
        if (!f.exists()) {
            return;
        }
        if (f.isDirectory()) {
            for (File f1 : f.listFiles()) {
                recursivelyDelete(f1);
            }
            if (!f.delete()) {
                throw new IOException("Unable to delete directory " + f);
            }
        }
        else {
            if (!f.delete()) {
                throw new IOException("Unable to delete file " + f);
            }
        }
    }
734433
public static void deleteDir(Path dir) throws IOException {
		Validate.isTrue(isDirExists(dir), "%s is not exist or not a dir", dir);

		// 后序遍历，先删掉子目录中的文件/目录
		Files.walkFileTree(dir, deleteFileVisitor);
	}
173970
public static boolean deleteDir(File dir) {
		if (dir != null && dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				if (!deleteDir(new File(dir, children[i])))
					return false;
			}
		}
		return dir.delete();
	}
339504
public static void deleteFiles(File dir, FilenameFilter filter) {
        // validations
        if(dir == null) {
            throw new DataUtilException("The delete directory parameter can not be a null value");
        } else if(!dir.exists() || !dir.isDirectory()) {
            throw new DataUtilException("The delete directory does not exist: " + dir.getAbsolutePath());
        }

        // delete files
        File [] files;
        if(filter == null) {
            files = dir.listFiles();
        } else {
            files = dir.listFiles(filter);
        }
        if (files != null) {
            for(File file : files) {
                if(file.isFile()) {
                    if(!file.delete()) {
                        throw new DataUtilException("Failed to delete file: " + file.getAbsolutePath());
                    }
                }
            }
        }
    }
757733
def rmtree(directory: str) -> None:
    
    log.debug("Deleting directory {!r}", directory)
    shutil.rmtree(directory, onerror=shutil_rmtree_onerror)
225566
public static boolean deleteFile(final File file) {
		// delete directory content recursively
		if (file.isDirectory()) {
			final File[] children = file.listFiles();
			for (File child : children) {
				if (!deleteFile(child)) {
					return false;
				}
			}
		}

		// delete file/empty directory
		return file.delete();
	}
844684
public static boolean clearDirectory(File directory) {
    if (directory.isDirectory()) {
      for (File file : directory.listFiles()) {
        if (!deleteRecursive(file)) {
          return false;
        }
      }
    }
    return true;
  }