# Recursively delete directory

**URL:** https://discourse.processing.org/t/recursively-delete-directory/3766
**Category:** Coding Questions
**Created:** [September 23, 2018, 3:45pm UTC](https://discourse.processing.org/t/recursively-delete-directory/3766 "2018-09-23T15:45:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![elaner](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/elaner/32/413_2.png) [@elaner](https://discourse.processing.org/u/elaner)
#### Post date: [September 23, 2018, 3:45pm UTC](https://discourse.processing.org/t/recursively-delete-directory/3766/1 "2018-09-23T15:45:57Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [September 23, 2018, 7:48pm UTC](https://discourse.processing.org/t/recursively-delete-directory/3766/2 "2018-09-23T19:48:36Z")

</div>

I believe you can check the documentation of [File](https://docs.oracle.com/javase/7/docs/api/java/io/File.html#delete()). 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.

Kf

---

<div class="post-metadata">

### Author: ![elaner](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/elaner/32/413_2.png) [@elaner](https://discourse.processing.org/u/elaner)
#### Post date: [September 23, 2018, 9:28pm UTC](https://discourse.processing.org/t/recursively-delete-directory/3766/4 "2018-09-23T21:28:50Z")

</div>

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);

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

```

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [September 25, 2018, 5:41pm UTC](https://discourse.processing.org/t/recursively-delete-directory/3766/5 "2018-09-25T17:41:33Z")

</div>

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?

```auto
  import java.io.File;
  String fileName = dataPath("deleteme.txt");
  File f = new File(fileName);
  if (f.exists()) {
    f.delete();
    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:

[https://docs.oracle.com/javase/7/docs/api/overview-summary.html](https://docs.oracle.com/javase/7/docs/api/overview-summary.html)
