NuGet packages are an essential part of modern .NET development, but package caches, outdated dependencies, and incomplete restores can sometimes cause unexpected build errors.

If Visual Studio is showing strange NuGet errors, missing references, package version warnings, or a project that refuses to build after changing package versions, cleaning and restoring the NuGet packages can often help.

In this guide, you'll learn how to clean and restore NuGet packages in Visual Studio, clear the NuGet cache, restore packages from the command line, and troubleshoot common package-related problems.


๐Ÿงน What Does Cleaning NuGet Packages Mean?

Cleaning NuGet packages means removing or refreshing package-related files that may be cached locally or stored as part of your project's build output.

This can be useful when package files become outdated, incomplete, or inconsistent with your project's current package references.

Cleaning your project and restoring packages gives NuGet an opportunity to resolve and download the required dependencies again.


๐Ÿ”„ What Does NuGet Restore Do?

NuGet restore examines your project's package dependencies and makes sure the required packages are available for the project.

For a typical .NET project, package references are defined in the .csproj file.

<ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

During restore, NuGet resolves the required package and its dependencies.


๐Ÿ› ๏ธ Method 1: Clean the Solution in Visual Studio

The first thing to try when troubleshooting a package-related build problem is to clean the solution.

In Visual Studio, open the:

Build โ†’ Clean Solution

This removes generated build output such as compiled binaries and intermediate files.

After cleaning the solution, build it again:

Build โ†’ Rebuild Solution

This forces Visual Studio to rebuild the project instead of relying on previously generated build output.


๐Ÿ“ฆ Method 2: Restore NuGet Packages from Visual Studio

Visual Studio provides a built-in option to restore NuGet packages.

In Solution Explorer, right-click your solution and select:

Restore NuGet Packages

Visual Studio will restore the packages required by the projects in the solution.

After the restore completes, try rebuilding the solution.


โš™๏ธ Method 3: Restore Packages from the Command Line

You can also restore NuGet packages using the .NET CLI.

dotnet restore

Run this command from the directory containing your solution or project.

You can also specify the project or solution explicitly:

dotnet restore MySolution.sln

Or:

dotnet restore MyProject.csproj

๐Ÿงฝ Method 4: Clear the NuGet Cache

If restoring packages does not solve the problem, you can clear the local NuGet caches.

Open a Developer Command Prompt, PowerShell window, or terminal and run:

dotnet nuget locals all --clear

This clears the local NuGet caches used by the .NET tooling.

You can then restore the packages again:

dotnet restore

This causes NuGet to retrieve the required packages again instead of relying on the previously cached copies.


๐Ÿ” Check Your NuGet Cache Locations

If you want to see where NuGet stores its local data, use:

dotnet nuget locals all --list

This displays the locations of the available NuGet caches.


๐Ÿ—‘๏ธ Method 5: Delete bin and obj Folders

Sometimes the problem isn't the NuGet cache itself. Old build artifacts inside your project can also cause confusing build behavior.

Close Visual Studio and delete the following folders from the affected project:

bin
obj

For example:

MyProject/
โ”‚
โ”œโ”€โ”€ bin/
โ”œโ”€โ”€ obj/
โ”œโ”€โ”€ MyProject.csproj
โ””โ”€โ”€ Program.cs

After deleting those folders, reopen the solution and run:

dotnet restore

Then rebuild the project.


๐Ÿ”ง Method 6: Restore Using Visual Studio's Package Manager Console

Visual Studio also provides the NuGet Package Manager Console.

Open:

Tools โ†’ NuGet Package Manager โ†’ Package Manager Console

You can then use NuGet commands when appropriate for your project.

For example:

Update-Package -reinstall

This command can reinstall package references in projects that use the corresponding NuGet package management workflow.

However, don't use package reinstallation as the first solution for every NuGet problem. Start with a normal restore and investigate the actual dependency error first.


โš ๏ธ When Should You Clear the NuGet Cache?

Clearing the cache should generally be considered a troubleshooting step, not something you need to do every time you build a project.

It can be useful when:


๐Ÿšจ Cleaning NuGet Packages Does Not Fix Every Conflict

It's important to understand that cleaning and restoring packages does not automatically fix dependency conflicts.

For example, suppose your project has:

PackageA
    โ””โ”€โ”€ CommonPackage 1.0

PackageB
    โ””โ”€โ”€ CommonPackage 2.0

Clearing the NuGet cache does not change the dependency requirements of PackageA or PackageB.

If NuGet reports a genuine version conflict, you need to investigate the dependency graph and determine which package is introducing the conflicting dependency.

For that situation, see:

๐Ÿ“ฆ How to Fix NuGet Package Version Conflicts in Visual Studio


๐Ÿ”Ž Find Which Package Is Causing the Problem

When a restore produces a dependency warning or error, inspecting transitive dependencies can help identify the package responsible.

dotnet list package --include-transitive

This can show packages that your project depends on indirectly.

You can then investigate which direct dependency introduced the package with the conflicting version.

For a more detailed guide, see:

๐Ÿ” How to Check Which NuGet Package Is Causing a Dependency Conflict


๐Ÿงช A Complete Clean and Restore Process

If you're experiencing persistent NuGet problems, you can follow this sequence.

Step 1 โ€” Close Visual Studio

Close Visual Studio before manually removing project build folders.

Step 2 โ€” Delete bin and obj

bin
obj

Step 3 โ€” Clear the NuGet cache

dotnet nuget locals all --clear

Step 4 โ€” Restore packages

dotnet restore

Step 5 โ€” Reopen Visual Studio

Open the solution again and allow Visual Studio to finish loading the restored dependencies.

Step 6 โ€” Rebuild the solution

Build โ†’ Rebuild Solution

If the problem still exists, look at the actual NuGet error instead of repeatedly clearing the cache.


๐Ÿ“‹ Common NuGet Problems and Solutions

Problem First Thing to Try
Package appears missing Run dotnet restore
Build output appears stale Clean the solution and rebuild
Package cache may be corrupted Clear the NuGet cache
Persistent dependency conflict Inspect direct and transitive dependencies
Different projects use different package versions Consider Central Package Management

๐Ÿ“š Related NuGet Articles

๐Ÿ“ฆ How to Fix NuGet Package Version Conflicts in Visual Studio

๐Ÿ”ง How to Fix NU1107: Version Conflict Detected for NuGet Package

๐Ÿ” How to Check Which NuGet Package Is Causing a Dependency Conflict

๐Ÿ“ฆ Direct vs Transitive NuGet Dependencies: What's the Difference?

๐Ÿ”„ How to Update NuGet Packages Safely Without Breaking Your Project

๐Ÿ“ฆ PackageReference vs packages.config: Which Should You Use?

๐Ÿ“ฆ How to Use Central Package Management in .NET


๐ŸŽฏ Conclusion

Cleaning and restoring NuGet packages is a useful troubleshooting technique when Visual Studio or the .NET CLI behaves unexpectedly.

Start with a normal restore and rebuild. If the problem persists, remove the project's bin and obj folders and consider clearing the local NuGet cache.

However, remember that cleaning the cache does not resolve genuine dependency conflicts. If NuGet reports a package version conflict, investigate the direct and transitive dependencies involved.

๐Ÿงน NuGet still giving you trouble?

Clean the project, restore the packages, and then investigate the actual dependency error if the problem continues.

๐Ÿค– AlgoLassi Assistant Have a question about this tutorial?

Ask AlgoLassi and get an answer plus the tutorials worth studying next.

Ask a question

๐Ÿ’ฌ Comments

Sign in with Google to publish immediately, or comment anonymously and wait for approval.

Comments will appear here when available.