Gradle - Delete files with certain extension

Use the Gradle Delete task.

task deleteFiles(type: Delete) {
    delete fileTree('dir/foo') {
        include '**/*.ext'
    }
}

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() }

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' }
}

Tags:

Gradle