Suspicious 'delete' example in 1.12 (and older) documentation

The documentation of ‘delete’ (see [1]) is suspicious:

task makePretty(type: Delete) {
  delete 'uglyFolder', 'uglyFile'
}

According to my understanding, ‘uglyFolder’ and ‘uglyFile’ are deleted during project evaluation/configuration. While possible from a syntactical point of view, it demonstrates IMHO bad coding style. Don’t we all agree that evaluation/configuration is a read-only/side-effect free operation?

Thus I propose to change the example slightly into:

task makePretty(type: Delete) {
  doLast {
    delete 'uglyFolder', 'uglyFile'
  }
}

[1] http://www.gradle.org/docs/1.12/dsl/org.gradle.api.tasks.Delete.html

The documentation is correct. You just mark the files you want to delete in the configuration phase, but the actual delete operation happens in the execution phase. Your 2nd sample won’t work as expected, as “uglyFolder” and “uglyFile” won’t be deleted when calling makePretty

cheers, René

How do I recognize when an execution happens, i.e. when looking at a Gradle script, how can I get to know what when happens?

In this case, is it a speciality of the ‘delete’ type?

Is it a speciality of the ‘delete’ method?

Am I able to programmatically ask the build script when an action is executed?

Hi, here, ‘delete’ is a method of the Delete task that configures this task. It’s usual, that task is configured during configuration phase (that’s what the name of the phase is already indicating) and the task action itself happens during the execution phase.

This is nothing special of the Delete task. Take for example a copy task:

task myCopy(type:copy){
        from ('source')
        into('target')
   }

here we configure the copy task in the configuration phase using the from and into methods of the Copy task that forms the API of the Copy task. The copy operation itself happens in the execution phase. All typed tasks (all task types except DefaultTask) have a default action.

hope that helps!

Hi, got it - however, now I believe that the name of the method - namely ‘delete’ has been rather unluckily chosen.

You see, I associate with ‘delete’ the action of actually deleting something. IMHO, a better method name would have been ‘include’ (a name that could also be used in type ‘copy’ (or Copy), i.e.

task myDelete(type: Delete) {
    include file, folder
 }

I associate ‘include’ much more with the act of configuring something than ‘delete’.

Nevertheless, thanks for your help. Let’s close this issue as ‘resolved’.

I agree that “delete” is an unlucky choice.