# Disable particular task?

**URL:** <https://discuss.gradle.org/t/disable-particular-task/49747>\
**Category:** Help/Discuss\
**Created:** [October 28, 2024, 2:53pm UTC](https://discuss.gradle.org/t/disable-particular-task/49747 "2024-10-28T14:53:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wojtek](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/wojtek/32/11265_2.png) [@wojtek](https://discuss.gradle.org/u/wojtek)\
**Post date:** [October 28, 2024, 2:53pm UTC](https://discuss.gradle.org/t/disable-particular-task/49747/1 "2024-10-28T14:53:03Z")

</div>

Hi,  
I’m trying to disable certain tasks in build script. Based mostly on [android - How can I disable a task in build.gradle - Stack Overflow](https://stackoverflow.com/questions/47192746/how-can-i-disable-a-task-in-build-gradle) I tried:

```gradle
tasks.getByName("jsBrowserTest").enabled = false

```

but it fails with:

```gradle
Task with name 'jsBrowserTest' not found in root project 'xyz'.

```

How Can I recursively disable it for the whole project?

(running with `-x` disables it corretly)

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [October 28, 2024, 8:42pm UTC](https://discuss.gradle.org/t/disable-particular-task/49747/2 "2024-10-28T20:42:53Z")

</div>

`tasks.getByName` is a very bad idea, as it breaks task-configuration avoidance.  
Better would be `tasks.named("...") { enabled = false }` but it will have the same problem as it also only works if the task is already registered, but it seems that task is only executed later.  
To correctly disable even if registered later, you would use `tasks.named { it == "..."}.configureEach { enabled = false }`.

But be aware that `enabled = false` is not the same as `-x`.  
`-x` completely removes the task from the task list so also the tasks it depends on are not executed unless also depended upon by some other task.  
`enabled = false` just disables the actions for that task, but the dependencies are still executed.

---

<div class="post-metadata">

**Author:** ![wojtek](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/wojtek/32/11265_2.png) [@wojtek](https://discuss.gradle.org/u/wojtek)\
**Post date:** [October 29, 2024, 8:32am UTC](https://discuss.gradle.org/t/disable-particular-task/49747/3 "2024-10-29T08:32:11Z")

</div>

> [@Vampire](#):
>
> `tasks.getByName` is a very bad idea, as it breaks task-configuration avoidance.  
> Better would be `tasks.named("...") { enabled = false }` but it will have the same problem as it also only works if the task is already registered, but it seems that task is only executed later.  
> To correctly disable even if registered later, you would use `tasks.named { it == "..."}.configureEach { enabled = false }`.

For some reason it fails:

 ![Captura de pantalla 2024-10-29 a las 9.31.50](https://global.discourse-cdn.com/gradle/original/2X/8/8996c7c1c42eb8cdc2efd6c524ca1592114b7ce5.png)

> [@Vampire](#):
>
> But be aware that `enabled = false` is not the same as `-x`.  
> `-x` completely removes the task from the task list so also the tasks it depends on are not executed unless also depended upon by some other task.  
> `enabled = false` just disables the actions for that task, but the dependencies are still executed.

Is there a way to mimic `-x` with the configuration?

and/or make it configurable via gradle properties?

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [October 29, 2024, 11:17am UTC](https://discuss.gradle.org/t/disable-particular-task/49747/4 "2024-10-29T11:17:53Z")

</div>

> For some reason it fails:

some reason === your Gradle version is too old

In older version you can instead do `.matching { it.name == "..." }.configureEach { ... }`.

> Is there a way to mimic -x with the configuration?

Add to `gradle.startParameter.excludedTaskNames`, that is where `-x` arguments land and as long as you do it during configuration phase it is effective as it is only evaluated by Gradle after configuration phase.

---

<div class="post-metadata">

**Author:** ![wojtek](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/wojtek/32/11265_2.png) [@wojtek](https://discuss.gradle.org/u/wojtek)\
**Post date:** [October 31, 2024, 8:44am UTC](https://discuss.gradle.org/t/disable-particular-task/49747/5 "2024-10-31T08:44:13Z")

</div>

OK, gradle upgraded 🙂

> [@Vampire](#):
>
> Add to `gradle.startParameter.excludedTaskNames`, that is where `-x` arguments land and as long as you do it during configuration phase it is effective as it is only evaluated by Gradle after configuration phase.

Perfect, this is what I was looking for!
