Deleting large folders on remote machines
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 &amp; 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"));
    }
}