HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Deleting large folders on remote machines

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
deletingfolderslargemachinesremote

Problem

I am trying to delete a large folders (with many levels of subfolders) over a network (i.e, on a remote machine).

I have the following code and it takes 10 minutes. Is there a way to improve this drastically?

public static boolean deleteDirectory(File directory) {

        File[] files = directory.listFiles(); // Value is null if the file is not an directory or if the file doesn't exist
        if(null!=files && files.length>0){
            //Gets in here only if the file is directory
            long filesCount = files.length;
            for(int i=0; i<filesCount; i++) {
                deleteDirectory(files[i]);
            }
        }
        return(directory.delete());
    }

Solution

I decide to write my own function(using native process) to delete the files(tested on windows), no external dependencies on jars.

Implementation details for windows:

-
I am using robocopy because rmdir fails for complex or deeply nested file paths.

-
The logic to delete files on windows works like Collections.retainsAll().

import java.io.File;

import java.io.IOException;

public class RecursiveDelete {

    public static void main(String[] args) throws IOException {
        String dirPath = "C:" + File.separator + "dev_tools" + File.separator + "dir_to_delete";
        System.out.println("Delete folder path=" + dirPath);
        execute(dirPath);
    }

    public static void execute(String folderToDelete) throws IOException {
        String os = System.getProperty("os.name").toLowerCase();
        File emptyFolder = createEmptyDirectory();
        if (isWindows(os)) {
            Runtime.getRuntime().exec("cmd /c robocopy " + emptyFolder + " " + folderToDelete + " /purge & rmdir " + folderToDelete);
        } else if (isUnix(os) || isMac(os) || isSolaris(os)) {
            Runtime.getRuntime().exec("rm -r -f  " + folderToDelete);
        }
    }

    private static File createEmptyDirectory() {
        File emptyFolder = new File("EmptyFolder");
        if (!emptyFolder.exists()) {
            boolean isCreated = emptyFolder.mkdir();
            System.out.println("Is empty folder created ?" + isCreated);
            System.out.println("EmptyFolder path" + emptyFolder.getAbsolutePath());
        }

        return emptyFolder;
    }

    public static boolean isWindows(String os) {
        return (os.contains("win"));
    }

    public static boolean isUnix(String os) {
        return (os.contains("nix") || os.contains("aix") || os.contains("nux"));
    }

    public static boolean isSolaris(String os) {
        return (os.contains("sunos"));
    }

    public static boolean isMac(String os) {
        return (os.contains("mac"));
    }
}

Code Snippets

import java.io.File;

import java.io.IOException;

public class RecursiveDelete {

    public static void main(String[] args) throws IOException {
        String dirPath = "C:" + File.separator + "dev_tools" + File.separator + "dir_to_delete";
        System.out.println("Delete folder path=" + dirPath);
        execute(dirPath);
    }

    public static void execute(String folderToDelete) throws IOException {
        String os = System.getProperty("os.name").toLowerCase();
        File emptyFolder = createEmptyDirectory();
        if (isWindows(os)) {
            Runtime.getRuntime().exec("cmd /c robocopy " + emptyFolder + " " + folderToDelete + " /purge & rmdir " + folderToDelete);
        } else if (isUnix(os) || isMac(os) || isSolaris(os)) {
            Runtime.getRuntime().exec("rm -r -f  " + folderToDelete);
        }
    }

    private static File createEmptyDirectory() {
        File emptyFolder = new File("EmptyFolder");
        if (!emptyFolder.exists()) {
            boolean isCreated = emptyFolder.mkdir();
            System.out.println("Is empty folder created ?" + isCreated);
            System.out.println("EmptyFolder path" + emptyFolder.getAbsolutePath());
        }

        return emptyFolder;
    }

    public static boolean isWindows(String os) {
        return (os.contains("win"));
    }

    public static boolean isUnix(String os) {
        return (os.contains("nix") || os.contains("aix") || os.contains("nux"));
    }

    public static boolean isSolaris(String os) {
        return (os.contains("sunos"));
    }

    public static boolean isMac(String os) {
        return (os.contains("mac"));
    }
}

Context

StackExchange Code Review Q#23673, answer score: 2

Revisions (0)

No revisions yet.