Is there a way to delete prior run's output for a task?

I don’t want to delete what is currently marked as output for a task, but what was produced by the last run of the task.

This is to prevent a problem where someone forgets to clean, changes say the output path, runs again.

Currently my task deletes outputs.files first, but when someone changes the directory path of the output in between runs without a clean, this causes it to delete the /currently/ defined directory (which doesn’t exist yet). Gradle handles this gracefully and does not fail the build, but the old output from the previous run is left around and forces us to do a full clean build since clean will clean the now currently defined outputs or find out where these outputs have changed manually. A directory is a small think to find, but more granular results are really horrible, and when it takes a long time to build, this is pretty awful. I just want to delete the previous run’s outputs for that task, which I would think is a key requirement of a build framework.

I know gradle somewhere has access to this information because it has to use it for incremental task checking, but I’m wondering if there is something accessible in the api. I would think there needs to be to support users who want to write their own upToDateWhen criteria by including standard criteria and adding to it. Seems like a decent use case.


There is the method TaskOutputsInternal.getPreviousOutputs() which will give you what you expect. Note that this is an internal API, so it may change between gradle version. You can access it from your task via


Can you raise an issue in the Gradle Github repository describing your use-case, so we prioritize adding a public API for it?


I will add the issue to the github project and clean it up to be more concise.