Skip to content
DevOps

HowTo: Clear NuGet Cache on a Build Server

2 min read
HowTo: Clear NuGet Cache on a Build Server

When a build server runs as a Windows service, the NuGet cache is stored under the system service account's profile rather than a regular user profile. This makes it non-obvious to locate, and the standard per-user cache path will not help. This post covers where to find it and how to clear it — including the modern CLI approach.

:::note[Key Takeaways]

  • Windows service accounts store the NuGet cache under C:\Windows\System32\config\systemprofile (64-bit) or C:\Windows\SysWOW64\config\systemprofile (32-bit)
  • The modern cross-platform command dotnet nuget locals all --clear works on any self-hosted runner with the .NET SDK installed
  • The -NoCache flag on nuget install bypasses the cache entirely without clearing it :::

Why the Cache Location Is Different on a Build Server

On a developer machine, NuGet caches packages in the current user's AppData\Local\NuGet\Cache folder. When TeamCity, Jenkins, or another CI tool runs as a Windows service, however, the process runs under the SYSTEM account (or a dedicated service account), which has its own profile directory. Packages accumulate there and are invisible to normal user-level tooling.

This matters when you have stale or corrupt packages causing intermittent build failures, or when you need to free up disk space on a long-running build agent.

Clearing the Cache

Windows Service Account Paths (Self-Hosted Runners)

Navigate to the appropriate path for the bitness of the service process and delete the contents:

text
C:\Windows\System32\config\systemprofile\AppData\Local\NuGet\Cache
C:\Windows\SysWOW64\config\systemprofile\AppData\Local\NuGet\Cache

Both paths may exist depending on whether 32-bit or 64-bit processes are in use. Clearing both is safe.

If the build agent has the .NET SDK installed, the following command clears all NuGet local caches regardless of which account the process runs under:

Bash
dotnet nuget locals all --clear

This is the preferred approach for any agent running .NET 5 or later, and works identically on Windows, Linux, and macOS-hosted runners.

Bypassing the Cache at Install Time

If you want to skip the cache for a single install without clearing it, use the -NoCache flag with the legacy nuget.exe CLI:

Bash
nuget install Ninject -NoCache

This is useful for pinning a specific restore to go direct to the feed, but it does not resolve a corrupt cache — clearing it does.

David Christiansen
David Christiansen

Solution Architect with 30 years in cloud infrastructure, security, identity, and .NET engineering.

Related Posts