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

voiddeleteDirectoryContents(File directory)
Delete the files in directory.
Preconditions.checkArgument(directory.isDirectory(), "Not a directory: %s", directory);
File[] files = directory.listFiles();
if (files == null) {
    throw new IOException("Error listing files for " + directory);
for (File file : files) {
    deleteRecursively(file);
voiddeleteDirectoryContents(File directory)
Same as calling #deleteDirectoryContents(File,boolean) deleteDirectoryContents(directory, false) .
deleteDirectoryContents(directory, false);
voiddeleteDirectoryContents(File directory)
Wipes out all the a directory starting from a given directory.
Preconditions.checkArgument(directory.isDirectory(), "Not a directory: %s", directory);
Deque<File> stack = Queues.newArrayDeque();
stack.add(directory);
while (!stack.isEmpty()) {
    File file = stack.peekLast();
    File[] files = file.listFiles();
    if (files == null || files.length == 0) {
        file.delete();
...
voiddeleteDirectoryContents(File directory)
delete Directory Contents
if (!directory.getCanonicalPath().equals(directory.getAbsolutePath())) {
File[] files = directory.listFiles();
if (files == null) {
    throw new IOException("Error listing files for " + directory);
for (File file : files) {
    deleteRecursively(file);
...
booleandeleteDirectoryContents(final File dir)
Recursively deletes all files and folders within the directory dir.
if (dir == null || !dir.isDirectory()) {
    return false;
File[] files = dir.listFiles();
for (final File file : files) {
    if (file.isDirectory()) {
        if (!deleteDirectory(file)) {
            return false;
...
voiddeleteDirectoryContents(final File dir, int deleteDirLevel, int level)
delete Directory Contents
if ((dir == null) || !dir.isDirectory()) {
    return;
final File[] files = dir.listFiles();
if (files != null) {
    for (final File f : files) {
        if (f.isDirectory()) {
            deleteDirectoryContents(f, deleteDirLevel, level + 1);
...
voiddeleteFiles(File dir)
Deletes all files in a directory, but leaving the directory otherwise unmodified.
if (dir != null && dir.isDirectory()) {
    File[] files = dir.listFiles();
    if (files != null) {
        for (int i = 0; i < files.length; i++) {
            delete(files[i]);
voiddeleteFiles(File dir)
delete Files
File[] files = dir.listFiles();
if (files == null)
    return; 
for (int i = 0; i < files.length; i++) {
    File file = files[i];
    if (file.isDirectory())
        deleteFiles(file);
    file.delete();
...
voiddeleteFiles(File directory)
delete Files
if (directory != null) {
    for (File file : directory.listFiles()) {
        if (file.isDirectory()) {
            deleteDirectory(file);
        if (!file.delete()) {
            file.deleteOnExit();
booleandeleteFiles(File directory)
Deletes all files and directories in the specified directory.
boolean result = true;
if (directory.isDirectory()) {
    File[] list = directory.listFiles();
    for (int i = list.length; i-- > 0;) {
        File file = list[i];
        if (file.isFile()) {
            result = result && file.delete();
return result;