47

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

1
  • 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 5, 2014 at 18:39

5 Answers 5

82

Use the Gradle Delete task.

task deleteFiles(type: Delete) {
    delete fileTree('dir/foo') {
        include '**/*.ext'
    }
}
4
  • 6
    For me this did it: delete fileTree(dir:'dir/foo', include: '**.ext') (Gradle 4.1, 2017) Sep 22, 2017 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 } } Feb 22, 2018 at 13:39
  • @GuerinoRodella no need, there is already a project.delete() method for this purpose. Mar 21, 2018 at 3:23
  • Sorry, but you explanation looks wrong. That is just configuration and does not run until clean task called
    – Hubbitus
    Jul 29, 2019 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' }
}
2
  • What problem you are seeing?
    – Hubbitus
    Jul 23, 2019 at 16:17
  • Hi @Hubbitus, I explain it in the my answer ~ Jul 25, 2019 at 9:36
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() }
1
  • 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 5, 2014 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()
        }
    }
}
3
  • 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, 2019 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. Jul 26, 2019 at 2:58
  • Please try, if it is not clean task, it is not run. It is just task configuration
    – Hubbitus
    Jul 26, 2019 at 7: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.