Java Utililty Methods Delete Folder

List of utility methods to do Delete Folder

Description

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

Method

booleandeleteFolder(@Nonnull File file)
delete Folder
if (file.exists()) {
    boolean result = true;
    if (file.isDirectory()) {
        File[] contents = file.listFiles();
        if (contents != null) {
            for (File f : contents) {
                result = result && deleteFolder(f);
    return result && file.delete();
return false;
voiddeleteFolder(File dir, boolean deleteFolderItself)
Deletes given folder with all of its content
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
    File f = files[i];
    if (f.isDirectory()) {
        deleteFolder(f, true);
    } else if (f.isFile()) {
        if (!f.delete()) {
            System.err.println("Cannot delete file " + f.getAbsolutePath());
...
voiddeleteFolder(File file)
delete Folder
if (!file.isDirectory() || (file.isDirectory() && file.list().length == 0)) {
    file.delete();
} else {
    File[] listFiles = file.listFiles();
    for (File childFile : listFiles) {
        deleteFolder(childFile);
    file.delete();
...
booleandeleteFolder(File file)
delete Folder
File[] flist = null;
if (file == null) {
    return false;
if (file.isFile()) {
    return file.delete();
if (!file.isDirectory()) {
...
booleandeleteFolder(File file)
Used to delete a folder.
if (file.exists()) {
    boolean ret = true;
    if (file.isDirectory()) {
        for (File f : file.listFiles()) {
            ret = ret && deleteFolder(f);
    return ret && file.delete();
...
voiddeleteFolder(File file, boolean deleteParent)
delete Folder
File[] list = file.listFiles();
if (list == null || list.length < 1) {
    if (deleteParent)
        file.delete();
    return;
for (File ele : list) {
    if (ele.isDirectory())
...
voiddeleteFolder(File file, boolean withCurrentFolder)
Delete the sub folders, if true, will delete current folder also.
if (file.exists() && file.isDirectory()) {
    File files[] = file.listFiles();
    for (File file2 : files) {
        deleteFile(file2, true);
    if (withCurrentFolder) {
        file.delete();
booleandeleteFolder(File folder)
Deletes the folder and all of its contents
if (!folder.exists())
    return false;
if (!folder.isDirectory())
    return false;
if (!cleanFolder(folder))
    return false;
return folder.delete();
voiddeleteFolder(File folder)
This method deletes a folder including all subfolders and files.
if (folder.exists()) {
    for (File child : folder.listFiles()) {
        if (child.isDirectory()) {
            deleteFolder(child);
        } else {
            if (!child.delete()) {
                throw new IOException("Deletion of file: " + child.getAbsolutePath() + " failed.");
    if (!folder.delete()) {
        throw new IOException("Deletion of folder: " + folder.getAbsolutePath() + " failed.");
booleandeleteFolder(File folder)
delete Folder
return deleteFolderContents(folder) && folder.delete();