Java Utililty Methods Delete File Recursively

List of utility methods to do Delete File Recursively

Description

The list of methods to do Delete File Recursively are organized into topic(s).

Method

booleandeleteRecursively(File file)
delete Recursively
boolean success = true;
if (file.isDirectory()) {
    for (File f : file.listFiles()) {
        if (f.isDirectory()) {
            success = success && deleteRecursively(f);
        success = success && f.delete();
success = success && file.delete();
return success;
voiddeleteRecursively(File fileEntry)
Recursively deletes a directory or a file immediately.
if (fileEntry.isDirectory()) {
    File[] listFiles = fileEntry.listFiles();
    for (File file : listFiles) {
        deleteRecursively(file);
if (!fileEntry.delete()) {
    fileEntry.deleteOnExit();
...
voiddeleteRecursively(File fileOrDir)
delete Recursively
if (fileOrDir.isFile()) {
    deleteFile(fileOrDir);
} else if (fileOrDir.isDirectory()) {
    String[] names = fileOrDir.list();
    if (names != null) {
        for (String name : names) {
            deleteRecursively(new File(fileOrDir, name));
    if (!fileOrDir.delete()) {
        throw new IOException("Could not delete dir: " + fileOrDir);
} else {
    throw new IOException("Neither file nor dir: " + fileOrDir);
voiddeleteRecursively(File fileOrDir)
delete Recursively
if (fileOrDir.isDirectory())
    for (File f : fileOrDir.listFiles())
        deleteRecursively(f);
fileOrDir.delete();
booleandeleteRecursively(File fileToDelete)
Delete a directory recursively
boolean retval = true;
if (fileToDelete.isDirectory()) {
    for (File fileInDir : fileToDelete.listFiles()) {
        retval &= deleteRecursively(fileInDir);
retval &= fileToDelete.delete();
return retval;
...
voiddeleteRecursively(File fRoot)
Deletes all files and directories under the given directory.
if (fRoot == null) {
    throw new NullPointerException("Root folder is null.");
if (fRoot.isFile()) {
    if (!fRoot.delete()) {
        throw new IOException("Unable to delete file '" + fRoot.getAbsolutePath() + "'");
    return;
...
voiddeleteRecursively(File name)
delete Recursively
if (name.isDirectory()) {
    File[] subdirs = name.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return pathname.isDirectory();
    });
    for (int i = 0; i < subdirs.length; ++i) {
...
booleandeleteRecursively(File root)
Delete the supplied File - for directories, recursively delete any nested directories or files as well.
if (root != null && root.exists()) {
    if (root.isDirectory()) {
        File[] children = root.listFiles();
        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                deleteRecursively(children[i]);
    return root.delete();
return false;
booleandeleteRecursively(File root)
Delete the supplied File - for directories, recursively delete any nested directories or files as well.
if (root.exists()) {
    if (root.isDirectory()) {
        File[] children = root.listFiles();
        for (int i = 0; i < children.length; i++) {
            deleteRecursively(children[i]);
    return root.delete();
...
booleandeleteRecursively(File root, boolean deleteRoot)
Delete the supplied java.io.File - for directories, recursively delete any nested directories or files as well.
if (root != null && root.exists()) {
    if (root.isDirectory()) {
        File[] children = root.listFiles();
        if (children != null) {
            for (File aChildren : children) {
                deleteRecursively(aChildren);
    if (deleteRoot) {
        return root.delete();
    } else {
        return true;
return false;