In this tutorial, we are going to highlight how to clear Maven cache in Mac, Windows, and Linux.

First, we will showcase how to delete the cache manually. Then, we will see how to achieve the same objective using the built-in Maven dependency plugin.

Deleting Maven Local Cache Manually

By default, Maven stores dependencies locally in a hidden directory named .m2. However, we can configure Maven to use a custom folder.

Depending on the project, the cache can grow quite big. So clearing it from time to time can improve performance and save disk space by removing all the artifacts that we no longer use.

In case .m2 isn’t the default cache location, we can find the configured directory using the command line:

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

Clearing on Windows

The default location in Windows is:

    
        C:\Users\{username}\.m2
    

So, we can clear the cache by simply deleting the .m2/repository folder.

Delete maven cache on Windows

Please bear in mind that we need to install Maven first.

Clearing on Mac OS

For Mac-based systems, the location is a little bit different:

    
        /Users/{username}/.m2
    

We can select the repository folder, then press Command-Delete.

Alternatively, we can accomplish the same thing using a command line:

    
        rm -rf \/Users/{username}/.m2/repository
    

Now, if we run the mvn package command, Maven will generate and restore only the needed artifacts.

Clearing on Linux

Similarly, in Linux machines, Maven local repository is defaulted to:

    
        /home/{username}/.m2
    

So, we can delete the cache by simply deleting the /home/{username}/.m2/repository folder:

    
        rm -rf /home/{username}/.m2/repository
    

Clear Maven Cache using Dependency Plugin

Now that we know how to remove the cached artifacts manually, let’s see how we can do it using the dependency plugin.

Simply put, the plugin provides the dependency:purge-local-repository goal to purge artifacts from the local maven repository.

So, let’s use it in practice:

    
        C:\Users\Asus\GIT>cd mvn-project
        C:\Users\Asus\GIT\mvn-project>mvn dependency:purge-local-repository
        [INFO] Scanning for projects...
        Downloading from central: ...
        ...
        [INFO] Deleting 58 transitive dependencies for project mvn-project from C:\Users\Asus\.m2\repository ...
        [INFO] Re-resolving dependencies
        ...
        Downloading from central: ...
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD SUCCESS
        [INFO] ------------------------------------------------------------------------
    

As we can see, we need first to navigate to our Maven project to run the purge-local-repository goal.

mvn dependency:purge-local-repository resolves the entire dependency tree. Then it clears the cache and re-downloads the artifacts.

The purpose of resolving the dependency tree is to pre-download the missing dependencies. This is called the transitive dependency resolution.

So, to avoid this step, we can use the actTranstively flag:

    
        mvn dependency:purge-local-repository -DactTransitively=false
        [INFO] Scanning for projects...
        [INFO] Deleting 3 direct dependencies ...
        [INFO] Re-resolving dependencies
        ...
        Downloading from central: ...
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD SUCCESS
        [INFO] ------------------------------------------------------------------------
    

Lastly, we can pass the flag -DreResolve=false to skip the step of re-resolving the dependencies:

    
        mvn dependency:purge-local-repository -DactTransitively=false -DreResolve=false
        [INFO] Scanning for projects...
        [INFO] ---------------------< com.devwithus:mvn-project >----------------------
        [INFO] Building mvn-project 0.0.1-SNAPSHOT
        [INFO] --------------------------------[ jar ]---------------------------------
        [INFO]
        [INFO] Deleting 3 direct dependencies ...
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD SUCCESS
        [INFO] ------------------------------------------------------------------------
    

Delete Specific Dependencies

purge-local-repository offers a convenient way to purge some artifacts that are not part of our project.

To do so, we can use the DmanualInclude or manualIncludes to specify the dependencies we want to clear.

    
        mvn dependency:purge-local-repository -DmanualInclude="xxxgroupId:xxxartifactId, ..."
    

Conclusion

To sum it up, we have explored different ways to clear Maven cache in Windows, Mac OS, and Linux.

First, we have seen how to do it manually. Then, we illustrated how to accomplish the same objective using the dependency plugin.