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

voiddeleteDirectory(String directoryName)
delete Directory
File file = new File(directoryName);
file.delete();
voiddeleteDirectory(String directoryName)
Deletes the directory (and any files) represented by directoryName, if directory doesn't exits does nothing.
File directory = new File(directoryName);
if (directory.exists()) {
    if (directory.isDirectory()) {
        File[] files = directory.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory())
                    deleteDirectory(file.getAbsolutePath());
...
booleandeleteDirectory(String dirName)
delete Directory
return deleteDirectory(new File(dirName));
voiddeleteDirectory(String dirName)
Recursively deletes a directory
deleteDirectory(new File(dirName));
voiddeleteDirectory(String dirname, boolean recursive)
INTERNAL: Deletes the directory with the given name, recursively if specified.
deleteDirectory(new File(dirname), recursive);
voiddeleteDirectory(String fileName)
delete Directory
File directory = new File(fileName);
if (!directory.exists()) {
} else {
    try {
        deleteResursive(directory);
    } catch (IOException e) {
        e.printStackTrace();
booleandeleteDirectory(String folder)
delete Directory
String dir = folder;
if (!dir.endsWith(File.separator)) {
    dir = dir + File.separator;
File dirFile = new File(dir);
if (!dirFile.exists() || !dirFile.isDirectory()) {
    return false;
boolean flag = true;
File[] files = dirFile.listFiles();
for (File file : files) {
    if (file.isFile()) {
        flag = deleteFile(file.getAbsolutePath());
        if (!flag) {
            break;
    else {
        flag = deleteDirectory(file.getAbsolutePath());
        if (!flag) {
            break;
if (!flag) {
    return false;
if (dirFile.delete()) {
    return true;
} else {
    return false;
voiddeleteDirectory(String name)
delete Directory
File folder = new File(name);
if (folder.exists()) {
    File[] files = folder.listFiles();
    if (files != null) {
        for (File f : files) {
            if (f.isDirectory()) {
                deleteDirectory(f.getAbsolutePath());
            } else {
...
booleandeleteDirectoryContent(final String strDir)
Delete all files and directories in directory but do not delete the directory itself.
return ((strDir != null) && (strDir.length() > 0)) ? deleteDirectoryContent(new File(strDir)) : false;
booleandeleteDirectoryContents(File directory)
delete Directory Contents
Preconditions.checkArgument(directory.isDirectory(), "Not a directory: %s", directory);
if (isSymbolicLink(directory)) {
    return false;
boolean success = true;
for (File file : listFiles(directory)) {
    success = deleteRecursively(file) && success;
return success;