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

booleandeleteDirectory(String sPath)
delete Directory
boolean flag = false;
if (!sPath.endsWith(File.separator)) {
    sPath = sPath + File.separator;
File dirFile = new File(sPath);
if (!dirFile.exists() || !dirFile.isDirectory()) {
    return false;
flag = true;
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
    if (files[i].isFile()) {
        flag = files[i].delete();
        if (!flag)
            break;
    } else {
        flag = deleteDirectory(files[i].getAbsolutePath());
        if (!flag)
            break;
if (!flag)
    return false;
if (dirFile.delete()) {
    return true;
} else {
    return false;
booleandeleteDirectoryAndContents(final File srcPath)
This method deletes a given directory with its content.
final String files[] = srcPath.list();
boolean folderDelete = true;
if (files != null) {
    for (int index = 0; index < files.length; index++) {
        final String sFilePath = srcPath.getPath() + File.separator + files[index];
        final File fFilePath = new File(sFilePath);
        folderDelete = folderDelete & fFilePath.delete();
folderDelete = folderDelete & srcPath.delete();
return folderDelete;
voiddeleteDirectoryAndContentsRecursive(final File srcPath, final boolean deleteSrcDir)
This method deletes a given directory with its content.
if (srcPath.exists()) {
    final File[] files = srcPath.listFiles();
    if (files != null) {
        for (int index = 0; index < files.length; index++) {
            if (files[index].isDirectory()) {
                deleteDirectoryAndContentsRecursive(files[index], true);
            files[index].delete();
...
booleandeleteDirectoryContents(File path)
Delete all the folders and subdirectories of the given directory
if (path.exists()) {
    File[] files = path.listFiles();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            deleteDirectoryContents(files[i]);
            files[i].delete();
        } else {
            files[i].delete();
...
booleandeleteDirectoryContents(String directoryPath, boolean deleteChildDirectories)
Delete a directories contents.
final String eLabel = "FileUtils.deleteDirectoryContents: ";
try {
    File directory = new File(directoryPath);
    if (!directory.exists()) {
        return false;
    if (!directory.isDirectory()) {
        throw new Exception("Not a directory!!!");
...
voiddeleteDirectoryIncludeContent(String folderPath)
Delete directory including contents
deleteDirectoryIncludeContent(new File(folderPath));
booleandeleteDirectoryInternal(File path)
delete Directory Internal
if (!path.exists()) {
    return true;
File[] files = path.listFiles();
boolean succeeded = true;
for (File file : files) {
    if (file.isDirectory()) {
        succeeded &= deleteDirectoryInternal(file);
...
booleandeleteDirectoryRecursively(File path)
delete Directory Recursively
if (path.exists()) {
    File[] files = path.listFiles();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory())
            deleteDirectoryRecursively(files[i]);
        else
            files[i].delete();
return path.delete();
voiddeleteDirectoryRecursivly(String path)
delete Directory Recursivly
File file = new File(path);
deleteDirectoryRecursivly(file);
voiddeleteDirectoryTree(String directoryPath)
deletes a directory tree, not including the child of directoryPath.
long length = 0;
File root = new File(directoryPath);
if (!root.isDirectory()) {
    throw new Exception("The path " + root.getAbsolutePath() + " specified is not a directory");
if (!root.exists()) {
    throw new Exception("The path " + root.getAbsolutePath() + " does not exist.");
if (!root.canWrite()) {
    throw new Exception("Invalid write permissions on the path " + root.getAbsolutePath());
String[] listing = root.list();
Vector dirs = new Vector();
for (int i = 0; i < listing.length; i++) {
    File f = new File(directoryPath + "/" + listing[i]);
    if (f.isFile()) {
        if (!f.delete()) {
            throw new Exception("Could not delete " + f.getAbsolutePath());
    } else if (f.isDirectory()) {
        dirs.addElement(f);
for (int i = 0; i < dirs.size(); i++) {
    File f = (File) dirs.elementAt(i);
    deleteDirectoryTree(f.getAbsolutePath());
    if (!f.delete()) { 
        throw new Exception("Could not delete " + f.getAbsolutePath());