skip to content

Forcing Rerun: --rerun-tasks & --rerun

The --rerun-tasks and --rerun flags that bypass up-to-date checks and the cache for a whole build or a single task. A practical question about how you isolate a suspected caching bug.

on this pageshow

questions

5

What does the --rerun-tasks command-line flag do, and when would you reach for it?

level: juniorimportance: must knowfreq 60%

answer

  1. whole-build flag
  2. ignores up-to-date + cache
  3. graph/ordering unchanged
  4. not a clean (outputs kept)
  5. onlyIf still applies

basics

~10 s

--rerun-tasks tells Gradle to ignore up-to-date checks for the whole build and re-execute every task in the requested task graph, even tasks it considers UP-TO-DATE. Useful to force a clean re-run while debugging.

solid answer

~40 s

`--rerun-tasks` is a global build flag that disables Gradle's incremental up-to-date optimization for the entire invocation. Normally Gradle skips a task when its declared inputs and outputs are unchanged (marked `UP-TO-DATE`). With `--rerun-tasks`, Gradle still computes the task graph and respects task dependencies and ordering, but executes every task in it regardless of state. It also bypasses pulling outputs from the local/remote build cache (`FROM-CACHE`). It does **not** delete existing outputs first, so it's not the same as `clean`. Typical uses: diagnosing whether a stale up-to-date result is hiding a bug, validating that a task is actually reproducible, or working around incorrectly declared inputs/outputs. In CI you'd prefer fixing the declarations over routinely forcing reruns, since reruns throw away the incremental-build speedups.

code

bash · 5 lines
bash
# Re-execute every task in the graph, ignoring UP-TO-DATE and FROM-CACHE
gradle build --rerun-tasks

# Combine with clean if you also want outputs deleted first
gradle clean build --rerun-tasks

go deeper

for a junior

Know it forces all tasks in the build to run and ignores UP-TO-DATE.

for a middle

Distinguish it from clean and note it also bypasses the build cache; graph/ordering unchanged.

for a senior

Explain onlyIf is independent, and that needing it often signals mis-declared inputs/outputs to fix.

for a principal

Frame the cost: routinely forcing reruns negates incremental builds and cache hit-rate across the org's pipelines; treat as diagnostic only.

## What problem it solves Gradle is an **incremental build tool**. Before running a task it compares the task's declared **inputs** (source files, properties) and **outputs** against a snapshot from the previous run. If nothing relevant changed, it skips the task and prints `UP-TO-DATE`. If a matching result exists in the build cache, it prints `FROM-CACHE`. This is what makes repeat builds fast. Sometimes you want to defeat that optimization for one invocation — for example you suspect a task produced a wrong result but Gradle keeps skipping it, or you want to measure a true cold task execution. ## The flag `--rerun-tasks` is a **command-line option on the whole build**, not on a single task. It forces every task in the executed task graph to actually run, ignoring up-to-date checks and the build cache. What it does **not** change: - It does not alter the task graph: dependencies, ordering rules (`mustRunAfter`, `shouldRunAfter`, `finalizedBy`) and which tasks are selected all behave normally. - It does not delete prior outputs. If you need a truly empty starting point, run `clean` (or the relevant clean task) as well. - It does not change task logic; tasks that internally short-circuit (their own `onlyIf {}` returning false) are still skipped. ## Relationship to onlyIf `--rerun-tasks` bypasses *up-to-date* checking, but a task's `onlyIf { ... }` spec is a separate gate. If `onlyIf` returns `false`, the task is still `SKIPPED`. So `--rerun-tasks` is not an absolute "run everything no matter what". ## Practical guidance ```bash # Force a full re-run of the build's tasks, ignoring up-to-date and cache gradle build --rerun-tasks ``` Use it as a **debugging / verification tool**, not as a habit. Routinely passing `--rerun-tasks` in CI defeats the entire point of incremental builds and the build cache, making every pipeline slow. If you find you *need* it regularly, that's a signal a task has missing or wrong input/output declarations — fix those instead.

  • Does --rerun-tasks delete the previous outputs?
    No. It re-executes tasks but leaves existing output files in place; you'd add a clean task to wipe them. So it differs from running clean.
  • Will --rerun-tasks run a task whose onlyIf {} returns false?
    No. onlyIf is an independent gate from up-to-date checking. The task still shows SKIPPED. --rerun-tasks only defeats up-to-date/cache, not onlyIf.

saying these in an interview costs you the question

  • Claiming --rerun-tasks is equivalent to clean (it doesn't delete outputs).
  • Saying it reruns only the requested task (it affects the whole executed graph).
  • Recommending it as a permanent CI flag rather than a debugging aid.

context

open as a page

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

level: middleimportance: must knowfreq 50%

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.

open as a page

What does outputs.upToDateWhen { false } do, and what are the trade-offs of putting it in a build script?

level: middleimportance: should knowfreq 40%

basics

~10 s

It makes a task's up-to-date check always fail, so the task re-executes on every build instead of being skipped as UP-TO-DATE. It's a programmatic, permanent way to force rerun (unlike the one-shot CLI flags).

open as a page

What is @DisableCachingByDefault and how does disabling caching relate to forcing a task to rerun?

level: seniorimportance: should knowfreq 30%

basics

~20 s

@DisableCachingByDefault marks a task type as not cached by the build cache by default, so its outputs are never restored FROM-CACHE. That removes one path that could skip real work, but it does NOT disable up-to-date checking.

open as a page

A teammate adds --rerun-tasks to the CI build because a task 'keeps producing stale output'. How would you diagnose and respond?

level: seniorimportance: should knowfreq 35%

basics

~20 s

Forcing reruns in CI hides the real bug and kills incremental/cache speed. Find why the task skips when it shouldn't — usually missing or wrong input/output declarations — and fix the declaration so up-to-date checking is correct.

open as a page