Skip to content
Latchkey

mvn clean: Usage, Options & Common CI Errors

Wipe target/ for a fresh build - the clean lifecycle, not the default one.

clean is the goal of Maven’s separate clean lifecycle. It removes the build output directory (target/ by default), ensuring a build does not reuse stale class files or artifacts from a previous run.

What it does

clean (maven-clean-plugin) deletes the directory configured as build.directory, normally target/. It belongs to the clean lifecycle, which is why it is usually chained before a default-lifecycle phase, as in mvn clean install.

Common usage

Terminal
mvn clean
mvn clean package
mvn clean verify
mvn clean install -DskipTests

Common error in CI (and the fix)

Symptom: "Failed to clean project: Failed to delete .../target/..." often on Windows agents or when a process holds a file open. Cause: a file lock or permission issue on the output directory. Fix: stop any process holding the file (e.g. a running app or test JVM), ensure the workspace is writable, and on persistent locks set <failOnError>false</failOnError> for maven-clean-plugin or run with -Dmaven.clean.failOnError=false.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →