How to clear .m2 Maven folder?

It's perfectly safe to delete the folder .m2/repository as maven will re-download all the needed dependencies when needed except for your local projects. It there any other folder under .m2 taking space?

For your local projects, maven will complain about not finding them. In that case, you simply need to re-compile them and install them by running mvn clean install in each project folder. They will get uploaded to the repository.


Solution 1:

You can even create new folder in D drive or any other drive. And use it.

Not necessary that you use .m2 folder created by default.

Also change this to new path:

<localRepository>D:\.m2\repository_Product</localRepository>

Change setting as shown in image below:

Solution 2:

delete .m2 folder and then make sure that all the projects are having only new versions/required version of jar entry in pom.xml. Re-build the project. The .m2 folder will remain with only latest version

Tip: Keep your IDE and required projects open so that it will be prevented from deletion.

enter image description here


Maven downloads all dependencies to a folder .m2/repository in the user's home directory (i.e. C:\users\[your user name]\.m2\repository on Windows. When you delete this folder, Maven creates the folder and downloads the dependencies for the projects you're building from then on.

If you want to change the folder to another directory, you can put a Maven configuration file settings.xml in the .m2 directory. You can find a sample file in your Maven distribution in the conf directory. Then look for the entry localRepository which should be commented out like this:

<!-- localRepository
 | The path to the local repository maven will use to store artifacts.
 |
 | Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->

Activate it an set it to the path you like, e.g. make Maven download the dependencies to D:\myMavenRepository set it to

<localRepository>D:/myMavenRepository</localRepository>

For an Ubuntu/Linux environment, use the below command to point to a new m2 directory without deleting the existing one.

mvn clean install -Dmaven.repo.local=/home/ishara/newm2