Java Utililty Methods Delete File or Directory

List of utility methods to do Delete File or Directory

Description

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

Method

voiddeleteFileOrDir(File dir, int max)
delete File Or Dir
File[] listFiles = dir.listFiles();
if (listFiles != null && max > 0) {
    for (File file : listFiles) {
        deleteFileOrDir(file, max - 1);
dir.delete();
voiddeleteFileOrDir(File fd)
Deletes the directory or file fd from the file system.
boolean deleted = fd.delete();
if (!deleted) {
    String type = fd.isFile() ? "file" : "directory";
    throw new IllegalStateException("Unable to delete " + type + " '" + fd.getPath() + "'");
voiddeleteFileOrDir(File file)
Utility method used to delete the profile directory when run as a stand-alone application.
if (file.isDirectory()) {
    File[] childs = file.listFiles();
    for (File child : childs) {
        deleteFileOrDir(child);
file.delete();
voiddeleteFileOrDir(File file)
Utility method used to delete the profile directory when run as a stand-alone application.
if (file.isDirectory()) {
    for (File child : file.listFiles()) {
        deleteFileOrDir(child);
if (!file.delete()) {
    throw new IOException("Could not remove '" + file + "'!");
booleandeleteFileOrDir(File filehandle)
Returns true if all deletions were successful.
if (filehandle.isDirectory()) {
    String[] children = filehandle.list();
    for (int i = 0; i < children.length; i++) {
        boolean success = deleteFileOrDir(new File(filehandle, children[i]));
        if (!success) {
            return false;
boolean isdel = filehandle.delete();
if (!isdel) {
    filehandle.deleteOnExit();
return isdel;
voiddeleteFileOrDir(File instanceFileOrDir)
delete File Or Dir
if (instanceFileOrDir.isDirectory()) {
    deleteDirectory(instanceFileOrDir);
} else {
    if (!instanceFileOrDir.delete()) {
        throw new RuntimeException("BB Stoch model factory: Could not delete instance file "
                + instanceFileOrDir.getAbsolutePath());
booleandeleteFileOrDirectory(File dir)
delete a file or directory
if (!dir.exists())
    return true;
if (dir.isFile())
    return dir.delete();
File fis[] = dir.listFiles();
for (int i = 0; i < fis.length; i++)
    deleteFileOrDirectory(fis[i]);
return dir.delete();
...
voiddeleteFileOrDirectory(File file)
Delete a directory or file
if (file.delete())
    System.out.println("Deleted : " + file.getAbsolutePath());
else
    System.out.println("Failed to delete : " + file.getAbsolutePath());
voiddeleteFileOrDirectory(File fileOrDirectory)
delete File Or Directory
if (fileOrDirectory.isDirectory()) {
    for (File file : fileOrDirectory.listFiles())
        deleteFileOrDirectory(file);
if (!fileOrDirectory.delete()) {
    throw new IOException("Failed deleting " + fileOrDirectory);
voiddeleteFileOrDirectory(String fileName)
delete File Or Directory
File file = new File(fileName);
if (file.exists()) {
    deleteFileOrDirectory(file);
throw new IllegalArgumentException("file:[" + fileName + "] not exists,please check it!");