If Maven is using a stale, incomplete, or corrupted dependency, clearing its local cache is often the quickest fix. Maven stores downloaded dependencies and plugin files in a local repository, usually inside the .m2 directory in your home folder. You can remove one artifact, clear the entire repository, or use Maven’s dependency plugin to clean up dependencies for a specific project. The right method depends on whether you want to fix one build problem or make Maven download everything again.
Where Does Maven Store Its Cache?
Maven’s local repository is a directory on your computer where it keeps downloaded libraries, build plugins, and related metadata. The default location is:
-
Linux and macOS:
~/.m2/repository -
Windows:
%USERPROFILE%\.m2\repository
The repository contains files Maven may reuse across projects, so it can grow over time. It is separate from your project’s source code and build output. For example, running mvn clean removes the project’s usual target directory, but it does not clear the Maven cache.
Your local repository may be in a different location if your Maven settings specify one. Check ~/.m2/settings.xml or the equivalent file in your Windows user profile for a <localRepository> setting. If one is configured, that path—not the default directory—is the cache Maven uses.
Quickest Way to Clear the Entire Maven Cache
To make Maven download all dependencies and plugins again, delete the contents of the local repository. You can remove the whole repository directory; Maven will recreate it the next time a build needs to download files.
On Linux or macOS, run:
rm -rf ~/.m2/repository
On Windows Command Prompt, run:
rmdir /s /q "%USERPROFILE%\.m2\repository"
In PowerShell, you can use:
Remove-Item -Recurse -Force "$env:USERPROFILE\.m2\repository"
Close active Maven builds before deleting the directory. If an IDE such as IntelliJ IDEA or Eclipse is running a build in the background, stop that process first. Otherwise, files may be in use, or Maven may recreate some of the cache while you are removing it.
After clearing the cache, run your build again, for example mvn clean install. Maven will download the required dependencies and plugins from the repositories configured for your project. The first build can take longer than usual and requires network access.
Clear Only One Maven Dependency
Removing the entire repository is effective, but often unnecessary. If one library is causing the problem, delete only that library’s folder. Maven stores artifacts in a directory structure based on the group ID, artifact ID, and version.
For example, an artifact with the coordinates com.example:sample-library:1.2.0 is typically stored under a path like:
~/.m2/repository/com/example/sample-library/1.2.0/
On Windows, the same folder is usually inside %USERPROFILE%\.m2\repository\com\example\sample-library\1.2.0\. Delete the folder for the affected version, then rerun the build. Maven should fetch that artifact again when it is needed.
Use the dependency’s full coordinates to identify the correct folder. Removing a similarly named artifact or the wrong version may not solve the issue. If you are unsure, search the build output for the dependency’s group ID, artifact ID, and version, or inspect the project’s dependency tree.
Clear Project Dependencies with Maven
You can also ask Maven to remove dependencies used by the current project through the Maven Dependency Plugin. From the project directory, run:
mvn dependency:purge-local-repository
To have Maven resolve the project’s dependencies again after purging them, run:
mvn dependency:purge-local-repository -DreResolve=true
This approach is scoped to the project’s dependency information rather than deleting every file in your local repository. It is useful when you suspect one of the project’s dependencies is stale but want to preserve cached artifacts used by other projects. The plugin itself may need to be downloaded the first time you use it.
For a broad cleanup, deleting the local repository is more direct. For a problem limited to one project, the purge goal or manual removal of the specific artifact is usually less disruptive.
Refresh Maven Snapshots Without Deleting the Cache
If the issue is an outdated snapshot dependency, you may not need to clear the cache. A snapshot is a development version that can change over time. Maven may use a locally cached copy until it checks the remote repository for an update.
Run your build with the -U option:
mvn -U clean install
This tells Maven to check for updated snapshots and missing releases. It can help when a snapshot has been published again but your build still appears to use an older copy. The option does not erase every cached artifact, so it is not a general replacement for deleting a corrupted dependency folder.
If the dependency still does not update, verify that the repository hosting it is configured correctly and that the artifact was actually published. Also check whether your project or settings use a mirror that points Maven to a different repository than expected.
What Clearing the Maven Cache Does—and Does Not Do
Clearing the local repository removes downloaded copies of dependencies, plugins, and repository metadata. Maven will need to download needed files again, which can make the next build slower and use more network bandwidth. If you work offline or have limited access to a repository, consider whether you can restore connectivity before removing the cache.
Clearing the cache does not normally delete your project’s source files, the project’s pom.xml, or your Maven settings. It also does not remove build output from the project’s target directory; use mvn clean for that. Conversely, mvn clean does not clear dependencies from .m2/repository.
A cache reset also cannot fix every dependency problem. If Maven cannot reach a repository, the dependency is not published, the version is wrong, or your credentials are invalid, Maven may fail again after downloading nothing. Treat cache cleanup as a way to remove stale local files, not as a fix for repository configuration or network access.
Common Problems After Clearing the Cache
- The build fails while downloading files: Check your internet connection, proxy settings, repository URL, and any credentials required by a private repository.
-
The dependency still appears outdated: Confirm the version in the project’s
pom.xml, check whether it is a snapshot, and try building withmvn -U. -
Maven is using another cache directory: Inspect
settings.xmlfor a custom<localRepository>path and clear that directory instead. -
Permission errors occur: Check ownership and permissions on the
.m2directory. Avoid running Maven as a different user if that creates files your normal account cannot update. - The error returns immediately: Stop other Maven processes and IDE builds before removing files, then rerun the command and review the first error in the build output.
If you work behind a corporate proxy or use an internal artifact repository, make sure Maven is configured to access it before clearing the cache. Otherwise, Maven may be unable to download dependencies that were previously available locally. In team or CI environments, also check whether a build cache is restored by the pipeline, since that cache can reintroduce the same files.
Which Maven Cache Cleanup Method Should You Choose?
Use the least disruptive option that addresses the problem. If one dependency seems corrupted, delete its version folder. If you want to refresh the current project’s dependencies, use mvn dependency:purge-local-repository. If a snapshot is simply out of date, try mvn -U clean install. Delete the entire .m2/repository directory only when you want Maven to rebuild its local repository from scratch or when narrower cleanup has not worked.
Before removing files, check for active builds and confirm the repository path. After cleanup, run the project’s normal Maven command and allow time for required files to download again.
Conclusion
To clear the Maven cache, remove the default local repository at ~/.m2/repository on Linux or macOS, or %USERPROFILE%\.m2\repository on Windows. For a single dependency, delete only its artifact folder; for project-level cleanup, use the Maven Dependency Plugin. If you only need to check for updated snapshot dependencies, try mvn -U instead. Choosing the smallest effective cleanup keeps useful cached files in place while helping Maven resolve the dependency problem.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.