Maven: Purge Old Dependencies from Local Repository

An in-progress project almost always has “snapshot” builds performed by team members and build servers. The build artifacts typically accumulate over time as the better development tools, such as Maven and Gradle, have a built-in dependency management system. One of the dependency management system features is caching the dependency artifacts from the build and from 3rd parties used in the application. Not only do they cache the most recent version, they cache multiple versions of all the artifacts, including the old snapshots of the artifacts.

Caching the old snapshot build artifacts consumes disk space. Usually, it does not have a large disk space impact, however with multiple large applications it has a cumulative impact, especially on build servers. While disk space is cheap, a simple maintenance task reduces the problem for larger installations.

Simple Maintenance Task Solutions

Manual

One maintenance option is manually deleting the files – either everything in the repository directory (located in .m2/repository of the user’s home directory by default) or selected subdirectories to target specific artifacts.  One can schedule a repeating task reminder to periodically delete them.

Maven Plugin: dependency:purge-local-repository

Another maintenance option is using the maven-dependency-plugin. While commonly using this plug to analyze a module’s dependencies, it has many more options.  One of the options is a purge-local-repository goal that automates deleting targeted files in a local repo.

For example, to purge all dependencies for a module or multi-module application’s dependencies, and redownload only the current versions, run the Maven goal with no arguments:

mvn dependency:purge-local-repository

Two of the plugin’s useful configuration options are “snapshotsOnly” and “actTransitively”:

• snapshotsOnly: Often it is only desired to purge old snapshots; we usually want to keep the released versions. When that’s true, set “snapshotsOnly” to true.

• actTransitively: To have the plugin ignore the transitive dependencies (only purge the module’s directly declared dependencies), set “actTransitively” to false.

One additional option of potential interest is “reResolve” – to prevent the plugin from downloading the dependencies again, set it to false. However, it is important to note that the next build will redownload them again!

In Conclusion… Automate It!

To help with a build server’s maintenance, make a build server/CI job that periodically runs dependency:purge-local-repository.  While the run-frequency need depends per server, particularly on available disk space consumption pace, monthly or bi-monthly is usually often enough.

Since snapshots are typically the big space consumers and can quickly become outdated, consider starting with only purging snapshots and not transitive dependencies; then adjust further after initial successful purges. 

For example:

mvn dependency:purge-local-repository -DsnapshotsOnly=true -DactTransitively=false

Further Information

Refer to the plugin’s reference page for more information:

  • http://maven.apache.org/plugins/maven-dependency-plugin/purge-local-repository-mojo.html
  • https://maven.apache.org/plugins/maven-dependency-plugin/examples/purging-local-repository.html

About Intertech

Founded in 1991, Intertech delivers software development consulting to Fortune 500, Government, and Leading Technology institutions, along with real-world based corporate education services. Whether you are a company looking to partner with a team of technology leaders who provide solutions, mentor staff and add true business value, or a developer interested in working for a company that invests in its employees, we’d like to meet you. Learn more about us.