Part of: Mac disk cleanup for developers: every cache, what it costs to rebuild (Developers)
The .m2 folder is a hidden folder in your home folder: ~/.m2. Its repository subfolder is Maven’s local repository, a cache of every library and plugin Maven has downloaded. You can delete the repository. Maven downloads what it needs again on the next build. Keep settings.xml if you have one.
Maven is a build tool for Java projects. Folders with names that begin with a dot are hidden in Finder, which is why .m2 is easy to miss.
Find it and see its size
ls ~/.m2
du -sh ~/.m2/repository
The first command lists what is inside. On the Mac used to check this article, ~/.m2 held a repository folder and a wrapper folder. To open it in Finder, run open ~/.m2.
Maven lets you move the repository by setting a path in settings.xml. If ~/.m2/repository is missing or tiny and you do use Maven, look in ~/.m2/settings.xml for a localRepository entry.
What is inside
repository: downloaded libraries and plugins, organised by group and version. This is the large part.settings.xml: your own configuration, such as mirrors and credentials for private servers. Small, and not a cache.wrapper: copies of Maven versions downloaded by the Maven wrapper, a script some projects use to fetch the right Maven version.
Safe, risky, never
- Safe: the
repositoryfolder, as a whole or in part. Cost: every library your next build needs downloads again. - Risky: deleting it before you must build offline, or when a library lives only on a private server you cannot reach. Libraries that you built and installed locally with Maven’s install step exist only in this folder, so removing them means you have to build them again.
- Never: do not delete
settings.xmlas part of a cleanup. It may hold credentials and server settings that you cannot recover from the cache.
What it costs to get it back
The next build downloads libraries and plugins again. For a small project that is a minute. For a large project, or your first build after clearing everything, it can take much longer and use a lot of data. If you work on several projects, you pay for each one the first time you build it.
Locally installed artifacts are the trap. If you ran an install on a library of your own so another project could use it, it lives in ~/.m2/repository and nowhere else. Delete the folder and the other project cannot find it until you build the library again.
How to clean it
- Quit your IDE and any running build.
- Check the size and decide how much to remove.
- For a gentler approach, remove only subfolders of old libraries or groups you no longer use, instead of the whole
repository. - Move the chosen folders to the Trash in Finder and empty it. See why the Trash does not free space.
- Run a build of your main project to refill what you need.
Does it overlap with Gradle?
Not directly. Gradle has its own cache in ~/.gradle. A project can use both, and each downloads its own copies. Clearing one does not shrink the other. See how to clear the Gradle cache. Android projects use Gradle, so see Android Studio disk use too.
The full list of developer caches is in the developer cleanup guide.
Worked example: see what you would lose
Look inside the repository before you decide:
ls ~/.m2/repository
On the Mac used to check this article, the top level held folders such as com, org, io and net. These are the reversed domain names of the groups that publish libraries. Go one level deeper to see the names:
ls ~/.m2/repository/org
On that Mac this listed groups such as apache, assertj and codehaus. If you see a group that is your own company or project, check whether you built it locally. Libraries you installed yourself sit next to downloaded ones, and nothing marks them as special. A library you never published to a server is the part that cannot come back from the network.
A gentler step: remove one group
You do not need to remove the whole repository. Pick a group of old libraries and move that subfolder to the Trash. Maven downloads it again only if a project needs it. This is the lowest-cost way to trim the cache, and you can stop at any point.
If something goes wrong
- The build says it cannot resolve a dependency. You are offline, or the library is gone from its server. Reconnect, or restore the folder from the Trash.
- A project cannot find your own library. You removed a locally installed one. Build and install that library again.
- Maven cannot reach a private server. Check
settings.xml. It holds the server entries. Do not delete it. - The
wrapperfolder is large. Each Maven version the wrapper downloaded is stored there. Old ones can go, and the wrapper downloads them again if a project asks.
When not to clean
Do not clear the repository before a release build on a machine with no network. Do not clear it if you do not know which libraries you built yourself.
See the size in a scan
DiskJoy is our product. Its scan reads your disk and shows how large ~/.m2 is, read-only. Cleanup is not built yet, so DiskJoy does not delete it today.
Quick checklist
du -sh ~/.m2/repositoryto see the size.- Check
settings.xmland keep it. - Remember locally installed libraries.
- Move old subfolders, or the whole repository, to the Trash. Empty it.
Frequently asked questions
Where is the .m2 folder on a Mac?
It is a hidden folder named .m2 in your home folder, so the path is ~/.m2. The downloaded libraries are in ~/.m2/repository. Finder hides folders that start with a dot.
Can I delete the .m2 repository?
Yes. It is a cache of downloaded libraries, and Maven downloads what it needs again on the next build. Keep settings.xml if you have one in .m2, because it holds your own configuration.
Why is my .m2 folder so big?
Every library and plugin version Maven has ever downloaded for any project stays there, including old versions. It only grows unless you clean it.