Java Utililty Methods File Path Delete

List of utility methods to do File Path Delete

Description

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

Method

voiddeleteDir(File dir)
delete Dir
for (File file : dir.listFiles()) {
    if (!file.delete()) {
        file.deleteOnExit();
if (!dir.delete()) {
    dir.deleteOnExit();
voiddeleteDir(File dir)
Delete the given dir and recursively delete all of its children.
for (File f : dir.listFiles()) {
    if (f.isDirectory()) {
        deleteDir(f);
    } else {
        f.delete();
dir.delete();
...
booleandeleteDir(File dir, boolean deleteRoot)
delete Dir
if (dir.isDirectory()) {
    String[] children = dir.list();
    for (int i = 0; i < children.length; i++) {
        boolean success = deleteDir(new File(dir, children[i]), true);
        if (!success) {
            return false;
if (deleteRoot) {
    return dir.delete();
return true;
booleandeleteDir(File dir, boolean deleteSelf)
delete Dir
if (dir.isDirectory()) {
    String[] children = dir.list();
    for (String child : children) {
        boolean success = deleteDir(new File(dir, child), true);
        if (!success) {
            return false;
if (deleteSelf) {
    return dir.delete();
} else {
    return true;
booleandeleteDir(File dir, int depth, boolean deleteRootDirectory)
Deletes all files and subdirectories under dir.
int originalDepth = depth;
depth++;
if (dir.isDirectory()) {
    String[] children = dir.list();
    for (int i = 0; i < children.length; i++) {
        boolean success = deleteDir(new File(dir, children[i]), depth, deleteRootDirectory);
        if (!success) {
            return false;
...
voiddeleteDir(File dir, String dirPath)
delete Dir
if (dir.exists() || dir.isDirectory()) {
    String[] filesName = dir.list();
    for (int i = 0; i < filesName.length; i++) {
        String path = dirPath + File.separator + filesName[i];
        File fileToDelete = new File(path);
        if (dir.exists() && fileToDelete.isDirectory()) {
            deleteDir(fileToDelete, path);
        } else {
...
booleandeleteDir(File directory)
Deletes the given directory.
if (directory.isDirectory()) {
    File[] children = directory.listFiles();
    for (File file : children) {
        boolean success = deleteDir(file);
        if (!success) {
            return false;
return directory.delete();
voiddeleteDir(File directory)
delete Dir
if (directory.exists()) {
    String[] children = directory.list();
    if (children != null) {
        for (int i = 0; i < children.length; i++) {
            deleteDir(new File(directory, children[i]));
directory.delete();
booleandeleteDir(File directory)
Delete the directory associated with the File as path if empty
if (directory == null) {
    return true;
if (!directory.exists()) {
    return true;
if (!directory.isDirectory()) {
    return false;
...
booleandeleteDir(File directory)
delete Dir
boolean ok = true;
File[] children = directory.listFiles();
if (children != null) {
    for (File file : children) {
        if (!file.isDirectory()) {
            ok = ok && file.delete();
        } else {
            ok = ok && deleteDir(file);
...