To delete a file in Java, we use the delete method of java.io.File class. First construct a File object with the file you want to delete, and then call delete on the File object. If its deleted successfully, the method return true, otherwise return false, the cause of false deletion include the file does not exist or is using by another process.

 
package test;
import java.io.File;
 
public class TestDelete {
    public static void main ( String [] args ) {
        String file = "c:\\tmp\\testdelete\\foo.txt";
        File f = new File(file);
        if(f.exists() && f.delete()) {
            System.out.println ("File " + file + " deleted successfully" );
        } else {
            System.out.println ("Can not delete file: " +  file);
        }
    }
}
 
 

However this way you can only delete one file at a time, what if you want to delete all the files in a directory, maybe recursively, or only delete the files match a pattern.

There are two ways you can do this, do it pragmatically in Java code or call shell command. The later will be much easier, but not portable, the code works in windows will not work in Linux.

How to delete all files in a directory

To delete all files in a directory, we construct a File object which represents the target directory , then get a list of File objects with listFiles, then loop through the array, skip the sub directories and delete all the other things.

 
    public static void main (String [] args ) {
        String dir = "c:\\tmp\\testdelete";
        File f = new File(dir);
        if ( ! f.isDirectory()) {
            System.out.println ("Its not a directory: " +  dir);
            return;
        }
 
        File[] fileList = f.listFiles();
        for ( File file :  fileList) {
            if( file.isDirectory()) {
                continue;
            }
 
            if(file.delete()){
                System.out.println ("Deleting " + dir + "\\" + file.getName());
            }
        }
    }
 

How to delete file by its extension with Java regurlar expression

Sometimes we may want to delete file by pattern, for example delete all txt files, in shell we would do something like this

 
del *.txt
 

Its just a pattern matching or a filter, we can do the same thing in Java with regular expression. Here is a function can get the extension name from a file name:

 
    public static String getExtension(String name) {
        Pattern p = Pattern.compile("\\.[^.\\\\]*$"); 
        Matcher m = p.matcher(name);
        if ( m.find()) {
            System.out.println(m.group(0));
            return m.group(0).toLowerCase();
        }
        return null;
    }
 

Delete file recursively

Suppose you want remove all files from a directory and all its descendant directory, this will need a recursive through the directory tree and delete file.

 
    public static void deleteRecursively(String dir) {
        File f = new File(dir);
        if ( ! f.isDirectory()) {
            System.out.println ("Its not a directory: " +  dir);
            return;
        }
 
        File[] fileList = f.listFiles();
        for ( File file :  fileList) {
            if( file.isDirectory()) {
                deleteRecursively(dir + "\\" + file.getName());
                continue;
            }
 
            if(file.delete()) {
                System.out.println ("Deleting " + dir + "\\" + file.getName());
            }
        }
    }
 

This will delete all files recursively, but would not touch directories, if you need to also remove directories, just remove the continue statement, or add an option.

 
    public static void deleteRecursively(String dir, Boolean includeDir) {
        File f = new File(dir);
        if ( ! f.isDirectory()) {
            System.out.println ("Its not a directory: " +  dir);
            return;
        }
 
        File[] fileList = f.listFiles();
        for ( File file :  fileList) {
            if( file.isDirectory()) {
                deleteRecursively(dir + "\\" + file.getName());
                if (! includeDir) 
                    continue;
            }
 
            if(file.delete()) {
                System.out.println ("Deleting " + dir + "\\" + file.getName());
            }
        }        
    }