# Task replace actions

**URL:** <https://discuss.gradle.org/t/task-replace-actions/39349>\
**Category:** Help/Discuss\
**Created:** [March 17, 2021, 9:03pm UTC](https://discuss.gradle.org/t/task-replace-actions/39349 "2021-03-17T21:03:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rivancic](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/rivancic/32/9148_2.png) [@rivancic](https://discuss.gradle.org/u/rivancic)\
**Post date:** [March 17, 2021, 9:03pm UTC](https://discuss.gradle.org/t/task-replace-actions/39349/1 "2021-03-17T21:03:02Z")

</div>

Why does [Task.replace()](https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/TaskContainer.html#replace-java.lang.String-) method keep old defined actions. Documentation says it will replace any existing task. It obviously doesn’t do that.

For example if I use

```
tasks.register("taskToBeReplaced") {
  doLast {
    println("Action Old")
  }
}

tasks.replace("taskToBeReplaced").configure {
  doLast {
    print("Action New")
  }
}

```

Result is

```
> Task :taskToBeReplaced
Action Old
Action New

```

You see action of the replaced method is still present!

But if I add actions.clear() command in configuration block of replaced task then the result is as expected

```
tasks.register("taskToBeReplaced") {
  doLast {
    println("Action Old")
  }
}

tasks.replace("taskToBeReplaced").configure {
  actions.clear()
  doLast {
    print("Action New")
  }
}

```

Result:

```
> Task :taskToBeReplaced
Action New

```

I am using gradle wrapper 6.8.2, am I missing something. Documentation clearly says replace, I am assuming everything is replaced from the old task including a list of actions.

As then I can use following existing task and reconfigure it.

```
tasks.getByPath("taskToBeReplaced").configure {
  actions.clear()
  doLast {
    print("Action New")
  }
}

```

And Task.replace() method becomes obsolete.
