Skip to content

Maven's local repository keeps every version of every dependency

The .m2 folder grows with every build and nothing removes old versions. What is safe to delete, and the plugin that does it precisely.

5 min read

Maven's local repository is a permanent archive by design. Every dependency at every version any project has ever asked for is downloaded once and kept, on the reasonable theory that you might need it again. Across a few years of work that theory produces a very large folder.

Look before you delete

du -sh ~/.m2 2>/dev/null
du -sh ~/.m2/repository/* 2>/dev/null | sort -h | tail -15

The second command groups by organisation, which is usually enough to recognise what belongs to work you no longer do. A framework you used for one project in 2022 will have its own top level folder holding several versions.

Deleting all of it is safe, and slow

Nothing in ~/.m2/repository is unique to your machine. Every artefact came from a repository and can be downloaded again, so removing the folder costs a long first build rather than anything permanent. On a slow connection that first build is genuinely long, which is the only reason to be more surgical.

mvn dependency:purge-local-repository   # inside one project

That removes what the current project uses, which is useful for fixing a corrupted download rather than for reclaiming space. For space, the folders you want gone are the ones no current project references at all.

The pieces that are pure debris

  • _remote.repositories and .lastUpdated files. Bookkeeping from failed or partial downloads. Removing .lastUpdated files also fixes the common case where Maven refuses to retry a failed download.
  • Snapshot versions. Development builds that were superseded the day after they were downloaded. They accumulate faster than releases.
  • Old major versions of the same artefact. If nothing you build still asks for version 2, it is dead weight.
find ~/.m2 -name '*.lastUpdated' -delete
find ~/.m2/repository -type d -name '*-SNAPSHOT' | head -20

How this compares to the neighbours

A Java machine usually has a Gradle folder as well, holding both dependencies and a complete Gradle distribution per version, and if it does Android work then the emulator images are larger than either. Check all three together rather than one at a time.

Common questions

Is it safe to delete the .m2 folder?

Yes. Everything in the local repository came from a remote repository and is downloaded again on the next build. The only cost is a slow first build, which on a poor connection can be significant.

How do I clear the Maven cache on a Mac?

Delete ~/.m2/repository, or remove selected organisation folders inside it for a more surgical result. Inside a single project, mvn dependency:purge-local-repository clears what that project uses, which is aimed at fixing bad downloads rather than freeing space.

What are .lastUpdated files in .m2?

Markers left by failed or partial downloads. They are tiny individually and there can be thousands of them. Deleting them is safe and also makes Maven retry downloads it had given up on.

Why is my .m2 repository so large?

Because it keeps every version of every dependency any project has ever requested, including snapshot builds that were replaced immediately. Nothing removes old versions, so the folder only grows.

Read next