48

I am using Gradle and would like to delete all files with a certain extension. Is that something Gradle can do?

ThomYorkkke
  • 2,061
  • 3
  • 18
  • 26
  • 1
    Gradle can pretty much do anything that you can script in groovy. So, the real question is, "is there a gradle **task** that can delete files with a certain extension." If it isn't written as a task then you don't get the benefit of gradle's task graph (`myBuild.dependsOn('deleteFiles')`) and/or incremental builds. – kevinmm Dec 05 '14 at 18:39

5 Answers5

86

Use the Gradle Delete task.

task deleteFiles(type: Delete) {
    delete fileTree('dir/foo') {
        include '**/*.ext'
    }
}
Mark Vieira
  • 13,198
  • 4
  • 46
  • 39
  • 6
    For me this did it: `delete fileTree(dir:'dir/foo', include: '**.ext')` (Gradle 4.1, 2017) – lealceldeiro Sep 22 '17 at 19:19
  • How can I declare an external method of type delete? i.e. ext.deleteFiles = { type: Delete String... fileNames -> fileNames.each{ fileName -> delete $fileName } } – Guerino Rodella Feb 22 '18 at 13:39
  • @GuerinoRodella no need, there is already a [`project.delete()`](https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#delete-java.lang.Object...-) method for this purpose. – Mark Vieira Mar 21 '18 at 03:23
  • Sorry, but you explanation looks wrong. That is just configuration and does not run until `clean` task called – Hubbitus Jul 29 '19 at 16:48
6

You may customize default clean task to include other directories and files for deletion like:

clean{
  delete 'buildDir', 'generated'
}

If you want use glob, you may use fileTree for example, or any other convenient methods to list files:

clean{
  delete 'build', 'target', fileTree(dockerBuildDir) { include '**/*.rpm' }
}
Hubbitus
  • 5,161
  • 3
  • 41
  • 47
2

There are several ways to delete files with certain extension.In general,you must select some files;then filter some of them and finally delete reminding file.For example try this :

def tree = fileTree('${SOME_DIR}') 
tree.include '**/*.${SOME_EXT}'
tree.each { it.delete() }
vahidreza
  • 843
  • 1
  • 8
  • 19
  • 3
    Beware that this is essentially groovy code using gradle's Project.fileTree API. The code will always be executed during the configuration phase of a gradle build, unless enclosed in a custom task block. – kevinmm Dec 05 '14 at 18:49
0
clean {
  delete xxxx
}

The above clean is not so correct. Because it actually calls a method named clean by task name. The kind of method is normally used to configure the task and it happened in the configuration time not in the task-execute time.

The following is a more reasonable way if you need modify the default clean. It's my example to delete all files except one excludes. The delete action in clean task really happen in task-execute time now.

    clean {

    Task self = delegate
    self.deleteAllActions()
    self << {

        project.delete(fileTree(dir: buildDir).exclude("**/tmp/expandedArchives/org.jacoco.agent*/**"))

        def emptyDirs = []

        project.fileTree(dir: buildDir).visit {
            File f = it.file

            if (f.isDirectory() ) {
                def children = project.fileTree(f).filter { it.isFile() }.files
                if (children.size() == 0) {
                    emptyDirs << f
                }
            }
        }

        // reverse so that we do the deepest folders first
        emptyDirs.reverseEach {
            println "delete file: " + it + ", " + project.delete(it) //it.delete()
        }
    }
}
Victor Choy
  • 4,006
  • 28
  • 35
  • So hard... Why not just use `doLast`? And what incorrect with `clean { delete xxxx }` it is not called if you not call task `clean` – Hubbitus Jul 25 '19 at 13:26
  • Becuase the default clean task have included default delete actions. we need deleteAllActions(). self << { the action is just my situation, normally people can delete the file as they want }. clean { delete xxxx } will call delete if you not call task clean? I think the closure is called at the configuration time. – Victor Choy Jul 26 '19 at 02:58
  • Please try, if it is not clean task, it is not run. It is just task configuration – Hubbitus Jul 26 '19 at 07:36
0

The only thing that worked for me was:

// Include only files that have the '.ext' extension
project.delete(fileTree(dir: './path/to/directory', include: "**.ext"))

I was able to call this at the top-level of my Gradle file and didn't have to use any form of clean task.

You can even move up and out of the current base-level directory to an adjacent directory using ../sibling-directory-name.

Fearnbuster
  • 806
  • 2
  • 14
  • 19