How to clear Maven Cache

September 06, 2021 No comments maven clear cache

1. Introduction

In this article, we will focus on how to clear maven cache. We might need to do it to remove artifacts that are no longer used and with that operation gain some disk space.

There are at least two ways to do it:

  • removing local cache directory,
  • use the Maven dependency plugin.

2. Removing local Maven repository

How to clear Maven cache?

First, we need to locate our Maven repository folder, then simply remove it with all subdirectories. This operation will remove all Maven dependencies so with another build Maven will restore only needed libraries.

Where I could find the Maven local repository folder?

It depends on the operating system and also custom Maven settings. By default in Windows operation systems, Maven keeps dependencies on the following path:

C:\Users\<user_name>\.m2

on Mac OS:

/Users/<user_name>/.m2

and on Linux-based systems it will be:

/home/<user_name>/.m2

In case you have a problem with locating this folder, you can use the dedicated Maven command:

mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout

it will print your repository folder with full path.

3. Maven Dependency Plugin

Another way to clear Maven cache directory is to use Maven Dependency Plugin. The first thing to do is to navigate to your project root folder - where your pom.xml file is located and run the following command:

mvn dependency:purge-local-repository

Running this command will download artifacts that are not present in our local Maven cache to resolve the dependency tree, next deletes local cache and re-download the artifacts.

To avoid pre-downloading missing dependencies use actTransitively=false flag:

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

To simply clear the Maven cache without downloading artifacts and resolving dependencies use the following:

mvn dependency:purge-local-repository -DactTransitively=false -DreResolve=false

4. Conclusion

In this article, we focused on ways to clear Maven's local cache. First, we present a manual method based on simply removing a folder with all its content, next we showed the Maven-style way, using Maven dedicated commands with specified flags.

{{ message }}

{{ 'Comments are closed.' | trans }}