# Delete task in execution phase

**URL:** https://discuss.gradle.org/t/delete-task-in-execution-phase/6535
**Category:** Old Forum Archive
**Created:** [January 28, 2013, 12:08pm UTC](https://discuss.gradle.org/t/delete-task-in-execution-phase/6535 "2013-01-28T12:08:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Matthew\_Lowe1](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@Matthew\_Lowe1](https://discuss.gradle.org/u/Matthew_Lowe1)
#### Post date: [January 28, 2013, 12:08pm UTC](https://discuss.gradle.org/t/delete-task-in-execution-phase/6535/1 "2013-01-28T12:08:00Z")

</div>

Hi,

I would like to ask you for help. I somehow don’t understand, how to leverage Gradle’s Delete task in execution phase.

```gradle
task T1(dependsOn: T2)
<< {
    // some behaviour
}
  task T2(type: Delete) << {
   delete 'myTestFile.txt'
   // since this is in execution phase, it won't work as expected
}

```

But I don’t want to do this:

```gradle
task T2(type: Delete) {
   // no matter what task is executed, this is executed always :-(((((
   // I need it to bind to T1 task, which is run in execution (if user invokes it)
}

```

---

<div class="post-metadata">

### Author: ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)
#### Post date: [January 28, 2013, 12:12pm UTC](https://discuss.gradle.org/t/delete-task-in-execution-phase/6535/2 "2013-01-28T12:12:00Z")

</div>

What you want is:

```gradle
task T2(type: Delete) {
   delete 'myTestFile.txt'
}

```

Because T2 is a Delete task, the ‘delete’ method in the _configuration_ closure we have there means something different to what it would if it were not a Delete task.

In this case, you are specifying what should be deleted when the task runs. That is, you are _configuring_ the delete task. If the task was not a delete task, the delete method would actually be ‘Project.delete()’ which deletes the file right at that time.

---

<div class="post-metadata">

### Author: ![Matthew\_Lowe1](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@Matthew\_Lowe1](https://discuss.gradle.org/u/Matthew_Lowe1)
#### Post date: [January 28, 2013, 12:32pm UTC](https://discuss.gradle.org/t/delete-task-in-execution-phase/6535/3 "2013-01-28T12:32:00Z")

</div>

Yes, I think I have understanding of that. Problem is, that I want my delete task to be run only if particular execution phase task is run

example:

```gradle
task T2(type: Delete)
{
   delete 'myTestFile.txt'
}
  task T0()
<< {
    // some behaviour
}
  task T1(dependsOn: T2)
<< {
    // some behaviour which needs to invoke T2 first
}

```

If I invoke

```gradle
gradle T1

```

It’ll be error like

```gradle
Could not find property 'T2' on root project ...

```

And I only want my delete task T2 to be run IF task T1 is run. Otherwise it should not do anything else.

I’m not sure, if this is possible or I should rather write some helper task, which would delete desired directory/file for me in execution phase.

---

<div class="post-metadata">

### Author: ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)
#### Post date: [January 28, 2013, 12:36pm UTC](https://discuss.gradle.org/t/delete-task-in-execution-phase/6535/4 "2013-01-28T12:36:00Z")

</div>

If you want to delete something in execution phase, you an just do:

```gradle
project.delete(someFile)

```

Have a look at the docs for ‘Project.delete()’.

---

<div class="post-metadata">

### Author: ![Matthew\_Lowe1](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@Matthew\_Lowe1](https://discuss.gradle.org/u/Matthew_Lowe1)
#### Post date: [January 28, 2013, 12:41pm UTC](https://discuss.gradle.org/t/delete-task-in-execution-phase/6535/5 "2013-01-28T12:41:00Z")

</div>

_ashamed_ …

Your solution makes perfect sense. I’ve been so focused on Delete task and config. phase, that I completely overlooked this.

Thanks for help

---

<div class="post-metadata">

### Author: ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)
#### Post date: [January 28, 2013, 12:43pm UTC](https://discuss.gradle.org/t/delete-task-in-execution-phase/6535/6 "2013-01-28T12:43:00Z")

</div>

No probs, happy Gradling.
