# How to Delete a directory tree excluding certain sub-directory using Gradle?

**URL:** https://discuss.gradle.org/t/how-to-delete-a-directory-tree-excluding-certain-sub-directory-using-gradle/31369
**Category:** Help/Discuss
**Created:** [April 10, 2019, 11:03am UTC](https://discuss.gradle.org/t/how-to-delete-a-directory-tree-excluding-certain-sub-directory-using-gradle/31369 "2019-04-10T11:03:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![vatsalmevada](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vatsalmevada](https://discuss.gradle.org/u/vatsalmevada)
#### Post date: [April 10, 2019, 11:03am UTC](https://discuss.gradle.org/t/how-to-delete-a-directory-tree-excluding-certain-sub-directory-using-gradle/31369/1 "2019-04-10T11:03:22Z")

</div>

My directory structure is as below:

```
dir -|
     |
     - sub_dir1 -|
     | |
     | - file1
     |
     - sub_dir2 -|
     | |
     | - file2
     |
     - sub_dir3 -|
                 |
                 - file3

```

I want to recursively delete all content of `dir` except content of `sub-dir1`

So the expected resulting directory structure should be as below:

```
dir -|
     |
     - sub_dir1 -|
                 |
                 - file1

```

I have tried following code and it deleting all the files except the content of `sub_dir1`. However it is not deleting the other sub directories but only the files within those directories.

`delete fileTree(dir: "dir1").exclude("sub_dir1").include(' **/**')`

Result of above code:

```
dir -|
     |
     - sub_dir1 -|
     | |
     | - file1
     |
     - sub_dir2 
     |           
     |
     - sub_dir3

```

How can I delete these directories too along with the files contained by them?

---

<div class="post-metadata">

### Author: ![Comnir](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@Comnir](https://discuss.gradle.org/u/Comnir)
#### Post date: [April 13, 2019, 9:35am UTC](https://discuss.gradle.org/t/how-to-delete-a-directory-tree-excluding-certain-sub-directory-using-gradle/31369/2 "2019-04-13T09:35:59Z")

</div>

I tried this and it seems to work. Notice that instead of passing fileTree result, I call delete on each matching file in visit{}:

```gradle
task deleteFolder(type: Delete) {
    fileTree(dir: 'dir1').exclude("sub_dir1").visit { FileVisitDetails details ->
        delete details.file
    }
}

```

I didn’t understand why what you wrote didn’t work. When printing the files in the file tree with each {}, I saw it didn’t list any folder, even if I removed the exclude() call. When calling visit {} it did print the folders. The difference, related to folders, is mentioned in the [documentation](https://docs.gradle.org/current/javadoc/org/gradle/api/file/FileTree.html) of these methods.

---

<div class="post-metadata">

### Author: ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)
#### Post date: [April 13, 2019, 6:24pm UTC](https://discuss.gradle.org/t/how-to-delete-a-directory-tree-excluding-certain-sub-directory-using-gradle/31369/3 "2019-04-13T18:24:14Z")

</div>

```gradle
delete fileTree('dir1').matching {
   exclude 'sub_dir1/**' 
} 

```
