Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete an empty directory (or the directory with all contents recursively) in gradle?

Tags:

I can't figure out how to delete all contents of a directory.

For cleaning out a directory, I want to remove all files and directories inside it: I want to wipe everything there is inside (files and directories).

I tried this with the delete task, but I can't figure out to make it also remove directories and not just files. I've tried different ways to specify the directories, but nothing works.

task deleteGraphicsAssets(type:Delete) {     delete fileTree('src').include('**/*') } 

.

task deleteGraphicsAssets(type:Delete) {     delete fileTree('src').include('/') } 

.

task deleteGraphicsAssets(type:Delete) {     delete fileTree('src').include('*') } 

Any help appreciated!


Edit:

This works - yet it seems a bit like a hack.

task deleteGraphicsAssets(type: Delete) {     def dirName = "src"     delete dirName      doLast {         file(dirName).mkdirs()     } } 

I was looking for something like:

task deleteGraphicsAssets(type: Delete) {     deleteContentsOfDirectory "src" } 

or

task deleteGraphicsAssets(type: Delete) {     delete {dir : "src", keepRoot : true } } 
like image 376
treesAreEverywhere Avatar asked Oct 10 '13 15:10

treesAreEverywhere


People also ask

How do I delete a folder with all files inside it recursively?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

Which command can be used recursively to delete populated directories?

rmdir is a command-line utility for deleting empty directories, while with rm you can remove directories and their contents recursively.

How do you delete a directory recursively in Java?

Using Java I/O Package listFiles() method to list all files and sub-directories in the directory. For each file, we recursively call deleteDir() method. In the end, we delete the directory using File. delete() .

What does Gradle clean do?

clean will delete a build directory, all your classes will be removed for a fresh compile. assemble will create an archive (JAR) file from your Java project and will run all other tasks that require compile, test and so on.


2 Answers

To delete the src directory and all its contents:

task deleteGraphicsAssets(type: Delete) {     delete "src" } 
like image 75
Peter Niederwieser Avatar answered Sep 22 '22 05:09

Peter Niederwieser


Following will delete all content from the src folder but leaves the folder itself untouched:

task deleteGraphicsAssets(type: Delete) {     def dirName = "src"      file( dirName ).list().each{         f ->              delete "${dirName}/${f}"     } } 
like image 20
Heri Avatar answered Sep 20 '22 05:09

Heri