Java Utililty Methods Delete Directory

List of utility methods to do Delete Directory

Description

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

Method

booleandeleteDirectory(File file)
delete Directory
File[] list = file.listFiles();
boolean done = true;
for (File f : list) {
    done &= deleteFile(f);
return done;
voiddeleteDirectory(File file)
delete Directory
if (file.isDirectory()) {
    if (file.list().length == 0) {
        file.delete();
    } else {
        String files[] = file.list();
        for (String temp : files) {
            File fileDelete = new File(file, temp);
            deleteDirectory(fileDelete);
...
booleandeleteDirectory(File fileOrDir)
Delete a file system file or directory (and all the files and sub-directories it may contain).
if (fileOrDir.isDirectory()) {
    String[] children = fileOrDir.list();
    for (int i = 0; i < children.length; i++) {
        if (!deleteDirectory(new File(fileOrDir, children[i]))) {
            return false;
return fileOrDir.delete();
voiddeleteDirectory(File root)
delete Directory
if (root == null || !root.exists()) {
    return;
if (root.isFile()) {
    root.delete();
} else if (root.isDirectory()) {
    File[] children = root.listFiles();
    if (children != null) {
...
voiddeleteDirectory(File root)
Recursively delete a directory and its contents.
if (!root.exists()) {
    return;
File[] currList;
Stack<File> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
    if (stack.lastElement().isDirectory()) {
...
voiddeleteDirectory(File root)
This will recursively delete all contents under the given directory.
if (root.exists() && root.isDirectory()) {
    File[] files = root.listFiles();
    for (int i = 0; i < files.length; i++) {
        deleteDirectory(files[i]);
root.delete();
booleandeleteDirectory(File sourceDirectory, FilenameFilter filter, boolean recursive, boolean deleteItself)
Delete all the files with matching the specified FilenameFilter in the specified directory.
inputNotNull(sourceDirectory, filter);
if (!sourceDirectory.exists() || !sourceDirectory.canRead() || !sourceDirectory.isDirectory())
    throw new IllegalStateException("Source is not in a legal state.");
final File[] files = (filter != null ? sourceDirectory.listFiles(filter) : sourceDirectory.listFiles());
for (File file : files) {
    if (file.isDirectory()) {
        if (recursive)
            deleteDirectory(file, filter, recursive, deleteItself);
...
booleandeleteDirectory(final File dir)
Deletes a directory recursively.
boolean success = true;
if (dir != null && dir.exists()) {
    try {
        if (dir.isDirectory()) {
            for (final File file : dir.listFiles()) {
                if (file == null) {
                    return false;
                if (!deleteDirectory(file)) {
                    success = false;
                    return success;
        if (!dir.delete()) {
            success = false;
            return success;
        return success;
    } catch (Exception e) {
return false;
booleandeleteDirectory(final File dir)
Deletes the directory dir , and all of its contents.
if (!deleteDirectoryContents(dir)) {
    return false;
return dir.delete();
voiddeleteDirectory(final File dir)
delete Directory
if (!dir.exists())
    return;
if (dir.isFile())
    throw new IllegalStateException(dir + " is not a directory.");
process("rm", "-rf", dir.getAbsolutePath());
if (dir.exists()) {
    throw new IllegalStateException(
            "The removal of the dir " + dir.getAbsolutePath() + " failed. The dir still exists.");
...