Delete an existing directory. The directory needs to be empty before it can
be deleted.

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

    final Path aRealDir = _getUnifiedPath (aDir);

    // Does the directory not exist?
    if (!aRealDir.toFile ().isDirectory ())
      return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError (EFileIOOperation.DELETE_DIR, 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, aRealDir);

    return _perform (EFileIOOperation.DELETE_DIR, Files::delete, aRealDir);
  }
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) {}

    }
61060
public static boolean delete(File f) throws IOException
	{
		assert (f.exists());

		if (f.isDirectory())
		{
			FileUtils.deleteDirectory(f);
			return true;
		}
		else
		{
			return f.delete();
		}
	}
782762
public static boolean deleteDir(Path targetDirPath) {
        debug(log, "deleteDir", targetDirPath);
        return deleteBulkThenFalseList(
                JMCollections.getReversed(JMPath.getSubPathList(targetDirPath)))
                .size() == 0 && delete(targetDirPath);
    }
229254
public static boolean deleteDirectory(File dir)
    {

        if (dir.canWrite()) {
            File [] subFiles = dir.listFiles();

            for(int i=0; i<subFiles.length; i++) {
                if (subFiles[i].isDirectory())
                    deleteDirectory(subFiles[i]);
                else
                    subFiles[i].delete();
            }

        }

        return dir.delete();

    }
1308700
@Nonnull
  public static FileIOError deleteFile (@Nonnull final Path aFile)
  {
    ValueEnforcer.notNull (aFile, "Path");

    final Path aRealFile = _getUnifiedPath (aFile);

    if (!aRealFile.toFile ().isFile ())
      return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError (EFileIOOperation.DELETE_FILE, aRealFile);

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

    return _perform (EFileIOOperation.DELETE_FILE, Files::delete, aRealFile);
  }
368351
protected static void deleteDir(final File dir) {
    try {
      boolean _exists = dir.exists();
      boolean _not = (!_exists);
      if (_not) {
        return;
      }
      org.eclipse.xtext.util.Files.sweepFolder(dir);
      try {
        dir.delete();
      } finally {
      }
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
284963
public static boolean delete(final File file) {
        boolean delete = false;
        if (file != null && file.exists()) {
            // even if is a directory try to delete. maybe is empty or maybe is a *nix symbolic link
            delete = file.delete();
            if (!delete && file.isDirectory()) {
                File[] childs = file.listFiles();
                if (childs != null && childs.length > 0) {
                    for (File child : childs) {
                        delete(child);
                    }
                    // then try again as by now the directory can be empty
                    delete = file.delete();
                }
            }
        }
        return delete;
    }
125631
public PurgeResponse purgeDirectory(String directory) {
        return purge(new PurgeRequest().addTask(new PurgeTask().withDirectory(directory)));
    }
309058
private void deleteDir(File dir)
    {
        if (dir.exists() && dir.isDirectory())
        {
            File[] files = dir.listFiles();

            for (int idx = 0; idx < files.length; idx++)
            {
                if (!files[idx].exists())
                {
                    continue;
                }
                if (files[idx].isDirectory())
                {
                    deleteDir(files[idx]);
                }
                else
                {
                    files[idx].delete();
                }
            }
            dir.delete();
        }
    }
591440
@Nonnull
  public static FileIOError createDirIfNotExisting (@Nonnull final Path aDir)
  {
    final FileIOError aError = createDir (aDir);
    if (aError.getErrorCode ().equals (EFileIOErrorCode.TARGET_ALREADY_EXISTS))
      return aError.withoutErrorCode ();
    return aError;
  }
1077241
public static boolean delete(final File aDir) {
        if (aDir.exists() && aDir.listFiles() != null) {
            for (final File file : aDir.listFiles()) {
                if (file.isDirectory()) {
                    if (!delete(file)) {
                        LOGGER.error(MessageCodes.UTIL_012, file);
                    }
                } else {
                    if (file.exists() && !file.delete()) {
                        LOGGER.error(MessageCodes.UTIL_012, file);
                    }
                }
            }
        } else if (LOGGER.isDebugEnabled() && aDir.listFiles() == null) {
            LOGGER.debug(MessageCodes.UTIL_013, aDir);
        }

        return aDir.delete();
    }
164937
public static boolean deleteDirContents(final File dir) {
        if (dir.isDirectory()) {
            final String[] children = dir.list();
            for (final String aChildren : children) {
                final File child = new File(dir, aChildren);
                if (child.isDirectory()) {
                    // Delete the sub directories
                    if (!deleteDir(child)) {
                        return false;
                    }
                } else {
                    // Delete a single file in the directory
                    if (!child.delete()) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
844437
public static void cleanDirectory(File directory) throws IOException {
    requireNonNull(directory, DIRECTORY_CAN_NOT_BE_NULL);

    Path path = directory.toPath();
    if (!path.toFile().exists()) {
      return;
    }

    cleanDirectoryImpl(path);
  }
751497
private boolean delete(final File f) throws IOException {

		if (f.isDirectory()) {
			final File[] files = f.listFiles();
			if (files != null) {
				for (File file : files) {
					final boolean del = delete(file);
					if (!del) {
						return false;
					}
				}
			}
		} else {
			return f.delete();
		}

		// Now directory is empty
		return f.delete();
	}
814580
public static void delete(@NonNull String name, @NonNull File directory) throws CouchbaseLiteException {
        if (name == null) { throw new IllegalArgumentException("name cannot be null."); }
        if (directory == null) { throw new IllegalArgumentException("directory cannot be null."); }
        if (!exists(name, directory)) {
            throw new CouchbaseLiteException(
                CBLError.Domain.CBLITE,
                CBLError.Code.NOT_FOUND);
        }

        final File path = getDatabasePath(directory, name);
        try {
            Log.i(DOMAIN, "delete(): path=%s", path.toString());
            C4Database.deleteAtPath(path.getPath());
        }
        catch (LiteCoreException e) {
            throw CBLStatus.convertException(e);
        }
    }
518027
public static void deleteDirRecursive(File dir) throws IOException {
        File failed = deleteDirRecursiveInternal(dir, true);
        if (failed != null) {
            throw new IOException("Could not delete file: " + failed.getPath());
        }
    }