skip to content

How does the --rerun flag differ from --rerun-tasks, and how do you target a single task with it?

level: middleimportance: must knowfreq 50%

answer

  1. --rerun = one task (7.5+)
  2. --rerun-tasks = whole graph
  3. per-task option after task name
  4. dependencies unaffected by --rerun
  5. neither deletes outputs / overrides onlyIf

basics

~10 s

--rerun (built-in task option) forces a single, specific task to ignore its up-to-date/cache state, written as --rerun after the task name. --rerun-tasks forces every task in the whole build to re-execute.

solid answer

~40 s

`--rerun` is a **built-in per-task option** Gradle adds to every task, introduced in Gradle 7.5. You attach it to one task on the command line, e.g. `gradle compileJava --rerun`, and only that task ignores its up-to-date result and build-cache entry; its dependencies still behave normally (they run or are skipped per their own state). `--rerun-tasks`, by contrast, is a **global build flag** that disables up-to-date and cache for *every* task in the executed graph. So `--rerun` is surgical (one task), `--rerun-tasks` is a sledgehammer (the whole build). Use `--rerun` when you've localized the suspicious task and want a fast targeted re-run without throwing away incremental results for everything else. Neither deletes outputs or overrides `onlyIf`.

code

bash · 5 lines
bash
# Single task: only :compileKotlin is forced to rerun
gradle compileKotlin --rerun

# Whole build: every task in the graph reruns
gradle build --rerun-tasks

go deeper

for a junior

Know --rerun targets one task and --rerun-tasks the whole build.

for a middle

Place --rerun after the task name; explain dependencies are unaffected and it's built-in since 7.5.

for a senior

Relate --rerun to a one-off outputs.upToDateWhen{false}; pick surgical vs whole-build based on where staleness is.

for a principal

Advise teams to prefer --rerun for fast targeted diagnosis to preserve cache hit-rate, and to fix root-cause declarations.

## Two different scopes Gradle gives you two ways to force re-execution, and the distinction is **scope**: | Flag | Kind | Scope | |------|------|-------| | `--rerun-tasks` | global build option | every task in the executed graph | | `--rerun` | built-in **task** option | only the task it's attached to | ## --rerun (per-task) Since Gradle 7.5, every task automatically exposes a built-in `--rerun` option. You place it **after the task name** it applies to: ```bash gradle test --rerun # only :test ignores up-to-date/cache ``` Under the hood it's roughly equivalent to declaring `outputs.upToDateWhen { false }` for that single task for this one invocation: the task's up-to-date check is forced to fail, so it executes, and its cache entry is not restored. Dependency tasks are unaffected — they run or stay `UP-TO-DATE` based on their own state. Because it's a *task* option (not a global one), it binds to the task token immediately preceding/around it on the command line. If you list several tasks, only the one the option is associated with reruns. ## --rerun-tasks (whole build) `--rerun-tasks` is parsed as a global flag and applies to the entire build regardless of where it appears: ```bash gradle build --rerun-tasks # the whole graph re-executes ``` ## Choosing between them - You suspect **one** task is producing a stale/wrong result → `--rerun` (cheap, keeps everything else incremental). - You want a **clean verification** of the full pipeline, or you're not sure where the staleness is → `--rerun-tasks`. ## Shared limitations Both bypass only up-to-date/cache. Neither deletes existing outputs (combine with `clean` for that), and neither overrides a task's `onlyIf {}` gate.

  • When you pass --rerun to :test, do its upstream compile tasks also rerun?
    No. --rerun is per-task, so only :test is forced. Upstream tasks run or stay UP-TO-DATE based on their own input/output state.
  • Roughly how is --rerun implemented for the chosen task?
    It effectively forces that task's up-to-date check to fail for the invocation (like a one-off outputs.upToDateWhen { false }) and skips restoring its cache entry.
  • Since which Gradle version is the built-in --rerun task option available?
    Gradle 7.5 added the built-in per-task --rerun option; before that you'd use --rerun-tasks or a custom outputs.upToDateWhen.

saying these in an interview costs you the question

  • Saying --rerun and --rerun-tasks are interchangeable.
  • Claiming --rerun also reruns the target task's dependencies.
  • Thinking --rerun must be defined per-task in build scripts (it's a built-in option on all tasks).

context