Java Utililty Methods Recursive Delete

List of utility methods to do Recursive Delete

Description

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

Method

voidrecursiveDelete(File f)
Recursively delete files starting with the one that is given.
if (f == null)
    return;
if (!f.exists())
    return;
File parent = f.getParentFile();
if (parent == null) {
    throw new IllegalArgumentException("Cannot delete a root directory");
File moveTo = new File(parent, "rdelete" + System.currentTimeMillis());
boolean moved = f.renameTo(moveTo);
if (moved == true)
    f = moveTo;
simpleRecursiveDelete(f);
voidrecursiveDelete(File f)
Recursive delete
if (f != null && f.exists()) {
    File[] files = f.listFiles();
    if (files != null) {
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                recursiveDelete(files[i]);
            } else {
                if (!files[i].delete())
...
booleanrecursiveDelete(File file)
recursive Delete
boolean status = true;
if (file.isDirectory()) {
    File[] files = file.listFiles();
    for (File f : files) {
        if (!f.getName().equals(".") && !f.getName().equals("..")) {
            status = status && recursiveDelete(f);
return status && file.delete();
voidrecursiveDelete(File file)
Delete Folder with Sub-files
if (!file.exists()) {
    return;
if (file.isDirectory()) {
    for (File f : file.listFiles()) {
        recursiveDelete(f);
file.delete();
voidrecursiveDelete(File file)
Recursively delete the contents of a directory or just delete a file
if (!file.exists())
    return;
if (file.isDirectory()) {
    File[] contents = file.listFiles();
    for (int i = 0; i < contents.length; i++)
        recursiveDelete(contents[i]);
try {
...
booleanrecursiveDelete(File file)
recursive Delete
boolean success = true;
if (file != null && file.exists()) {
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        if (files != null) {
            for (File child : files) {
                recursiveDelete(child);
    success = file.delete();
return success;
booleanrecursiveDelete(File file)
Attempt to recursively delete the specified file or folder.
if (file != null && file.exists()) {
    if (file.isDirectory()) {
        for (File child : file.listFiles()) {
            if (!recursiveDelete(child)) {
                return false;
    return file.delete();
} else {
    return true;
voidrecursiveDelete(File file)
recursive Delete
if (file.isDirectory()) {
    if (file.list().length == 0) {
        file.delete();
    } else {
        String files[] = file.list();
        for (String f : files) {
            File fileToDel = new File(file, f);
            recursiveDelete(fileToDel);
...
booleanrecursiveDelete(File file)
recursive Delete
Deque<File> toDelete = new ArrayDeque<>();
toDelete.push(file);
while (!toDelete.isEmpty()) {
    File target = toDelete.pop();
    File[] contents = target.listFiles();
    if (contents == null || contents.length == 0) {
        if (target.exists() && !target.delete()) {
            return false;
...
voidrecursiveDelete(File file)
recursive Delete
if (file == null) {
    return;
File[] files = file.listFiles();
if (files != null) {
    for (File each : files) {
        recursiveDelete(each);
if (!file.delete()) {
    throw new IOException("Failed to remove " + file);