Java Utililty Methods Path Delete nio

List of utility methods to do Path Delete nio

Description

The list of methods to do Path Delete nio are organized into topic(s).

Method

voiddelete(Path targetPath)
delete
Files.delete(targetPath);
voiddelete(Path... paths)
Deletes all given Paths recursively.
for (Path path : paths) {
    if (!Files.exists(path)) {
        continue;
    List<Path> childPaths = collect(path);
    Collections.reverse(childPaths);
    for (Path childPath : childPaths) {
        Files.delete(childPath);
...
voiddelete(String filePath, String fileName)
delete
Path fileToDeletePath = Paths.get(filePath + File.separator + fileName);
boolean existsFile = fileToDeletePath.toFile().exists();
if (existsFile) {
    Files.delete(fileToDeletePath);
voiddelete(String targetFilePath)
delete
Path path = Paths.get(targetFilePath);
Files.deleteIfExists(path);
intdeleteAllFilesRecursively(Path path)
delete All Files Recursively
return deleteFilesRecursively(path, ALL_FILES);
voiddeleteContent(Path directory)
delete Content
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.delete(file);
        return FileVisitResult.CONTINUE;
});
voiddeleteEmptyDirsUpTo(Path from, Path to)
delete Empty Dirs Up To
Path pathsInBetween = to.relativize(from);
for (int i = pathsInBetween.getNameCount(); i > 0; i--) {
    Path toRemove = to.resolve(pathsInBetween.subpath(0, i));
    if (Files.list(toRemove).findAny().isPresent()) {
        return;
    } else {
        Files.delete(toRemove);
voiddeleteEmptyParentDirs(Path pkgDirPath, Path repoPath)
Delete empty parent directories.
Path pathsInBetween = repoPath.relativize(pkgDirPath);
for (int i = pathsInBetween.getNameCount(); i > 0; i--) {
    Path toRemove = repoPath.resolve(pathsInBetween.subpath(0, i));
    if (!Files.list(toRemove).findAny().isPresent()) {
        Files.delete(toRemove);
voiddeleteFile(Path p)
delete File
try {
    Files.deleteIfExists(p);
} catch (IOException e) {
    throw new RuntimeException(e);
voiddeleteFile(Path path)
Deletes a file if it exists.
if (!isFolder(path)) {
    Files.deleteIfExists(path);