Recursively delete directory

I need to delete a directory that has files in it. Anyone have code to do this? I have tried a number of Java routines I found online but without luck.

I believe you can check the documentation of File. I personally do not provide code to do this type of stuff. It is very easy to wipe your whole system is you don’t do it properly. Even if you are an expert, other forum member might not run with the same luck.


1 Like

Thanks, Kf. I did try File using some code I found online (below). But this code goes straight to the last line: println("Failed to delete " + file);

void deleteDirectoryRecursionJava6(File file) {
  if (file.isDirectory()) {
      File[] entries = file.listFiles();
      if (entries != null) {
        for (File entry : entries) {
    if (!file.delete()) {
      println("Failed to delete " + file);

I want to reiterate that recursive deleting can be very dangerous.

ANYONE READING THIS (not just original poster) – if you are testing having your sketch delete files, first confirm successful delete of a single named file, then print dry run info on what would have been deleted… then activate the code once you are sure it. Consider including checks so that if someone passes it an empty string on accident it doesn’t recursively delete from your user directory or the root of your file system. (!)

Are you already able to delete single named files, e.g. out of the sketch /data folder?

  String fileName = dataPath("deleteme.txt");
  File f = new File(fileName);
  if (f.exists()) {
    println("deleted:", fileName);

…also note that with Processing 3.4 you generally want reference code from Java 7 (and some Java 8) – so the best API to read is here: