Java Utililty Methods Delete Empty Directory

List of utility methods to do Delete Empty Directory

Description

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

Method

booleandeleteEmptyChildren(java.io.File file)
delete Empty Children
boolean b = true;
if (file.isDirectory()) {
    java.io.File[] children = file.listFiles();
    for (int i = 0; i < children.length; i++)
        b &= deleteEmptyFolders(children[i]);
return b;
booleandeleteEmptyDir(File directory)
delete Empty Dir
if (directory.exists() && directory.isDirectory() && directory.list() != null
        && directory.list().length == 0) {
    return directory.delete();
return false;
voiddeleteEmptyDir(File file)
delete Empty Dir
if (!file.exists() || file.isFile()) {
    return;
if (file.isDirectory()) {
    if (file.list().length == 0) {
        file.delete();
    } else {
        for (File f : file.listFiles()) {
...
intdeleteEmptyDirectories(List dirs)
Deletes empty directories recursively.
int dirsDeleted = 0;
boolean success = false;
File dir;
for (Iterator<File> dirIter = dirs.iterator(); dirIter.hasNext();) {
    dir = dirIter.next();
    if ((dir != null) && dir.exists() && (dir.list().length == 0)) {
        success = dir.delete();
        if (success) {
...
voiddeleteEmptyDirectory(File fileOrDirectory)
delete Empty Directory
if (fileOrDirectory.isDirectory()) {
    if (fileOrDirectory.list().length == 0) {
        fileOrDirectory.delete();
voiddeleteEmptyDirectoryRecursive(File directory)
Deletes an empty directory, including all parent directories that are also empty, stopping at the first non-empty parent.
if ((directory != null) && directory.isDirectory()) {
    File[] files = directory.listFiles();
    if ((files == null) || (files.length == 0)) {
        if (!directory.delete())
            throw new IOException("Could not delete empty directory: " + directory);
        deleteEmptyDirectoryRecursive(directory.getParentFile());
booleandeleteEmptyDirectoryRecursive(File directory)
delete Empty Directory Recursive
String canonicalParent;
try {
    canonicalParent = directory.getCanonicalPath();
} catch (IOException ioe) {
    return false;
if (!directory.isDirectory()) {
    return false;
...
voiddeleteEmptyDirs(File dir)
delete Empty Dirs
File[] files = dir.listFiles();
if (files != null) {
    for (int i = 0; i < files.length; i += 1) {
        deleteEmptyDirs(files[i]);
    dir.getCanonicalFile().delete();
voiddeleteEmptyDirs(File dir)
Deletes all empty directories in a directory, but leaves the directory itself unmodified.
File[] files = dir.listFiles();
if (files != null) {
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            deleteEmptyDirs(files[i]);
            File[] subfiles = files[i].listFiles();
            if (subfiles == null || subfiles.length == 0) {
                delete(files[i]);
...
voiddeleteEmptyFiles(String p)
delete Empty Files
File path = new File(p);
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
    if (files[i].length() == 0) {
        files[i].delete();