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

voiddeleteLockFile(Path logFile)
Removes any lck files for the given log file.
String lockFileName = logFile.getFileName().toString().concat(".lck");
Path parent = logFile.getParent();
Path lockFile;
if (parent == null) {
    lockFile = Paths.get(lockFileName);
} else {
    lockFile = parent.resolve(lockFileName);
Files.deleteIfExists(lockFile);
voiddeleteNotEmptyDirectory(Path path)
delete Not Empty Directory
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.delete(file);
        return CONTINUE;
    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
...
voiddeleteOnExit(Path path)
Register Path for a delete on JM shutdown event.
DELETE_ON_EXIT_PATHS.add(path);
voiddeletePath(Path path)
delete Path
if (path == null) {
    return;
File file = path.toFile();
if (file.isDirectory()) {
    for (File item : file.listFiles()) {
        deletePath(item.toPath());
deleteFile(file);
voiddeletePathRecursively(String path)
Deletes a path recursively.
Path root = Paths.get(path);
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.delete(file);
        return FileVisitResult.CONTINUE;
    @Override
...
voiddeleteQuietly(@Nullable Path path)
delete Quietly
if (path == null || !exists(path)) {
    return;
if (isRegularFile(path)) {
    try {
        doDelete(path);
    } catch (final IOException ignored) {
    return;
try {
    walkFileTree(path, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            try {
                doDelete(file);
            } catch (final IOException ignored) {
            return CONTINUE;
        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
            try {
                doDelete(dir);
            } catch (final IOException ignored) {
            return CONTINUE;
    });
} catch (final IOException ignored) {
voiddeleteQuietly(Path dir)
Deletes the directory recursively and quietly.
try {
    Files.walk(dir).sorted(Comparator.reverseOrder()).forEach(file -> {
        try {
            Files.delete(file);
        } catch (IOException e) {
    });
} catch (IOException e) {
...
voiddeleteRecursive(Path path)
Recursively delete the given path
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.delete(file);
        return FileVisitResult.CONTINUE;
    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
...
voiddeleteRecursive(Path path)
delete Recursive
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.delete(file);
        return FileVisitResult.CONTINUE;
    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
...
voiddeleteRecursively(final Path path)
delete Recursively
deleteRecursively(path, true);