121

I know I can uninstall-package from the PM console. I got into some dependency issues with another project and I want to start over, and I need to delete all packages in one shot. Is there a way?

Norman
  • 3,279
  • 3
  • 25
  • 42
dgorti
  • 1,221
  • 2
  • 9
  • 4

9 Answers9

177

To get all packages from all projects in the solution use Get-Package. To get all packages from a specific project use Get-Package -ProjectName "YourProjectName".


Remove all packages from all projects in the solution

Be careful: This will uninstall ALL packages in the solution. If -Force parameter is used, packages are removed even if dependencies exist.

Get-Package | Uninstall-Package -RemoveDependencies -Force


Remove all packages from a specific project within a solution

Be careful: This will uninstall ALL packages in the project. If -Force parameter is used, packages are removed even if dependencies exist.

Get-Package -ProjectName "YourProjectName" | 
Uninstall-Package -ProjectName "YourProjectName" -RemoveDependencies -Force
Norman
  • 3,279
  • 3
  • 25
  • 42
  • 29
    I recommend to add the -Force parameter at the end. Without it, I can't uninstall all my dependencies based on dependencies with other NuGet packages. – Sean Stayns Apr 22 '17 at 21:38
  • 1
    This is the real answer. Combined with Sean's suggestion to use `-force`. This worked for me. – Spikee Nov 26 '18 at 10:40
87

In Package Manager Console just type:

get-package | uninstall-package -removedependencies

36

try this:

get-package | uninstall-package -removedependencies -force
Amir
  • 2,259
  • 1
  • 19
  • 29
15

If you want to uninstall every NuGet Package from every Project in a Solution, then use this in the NuGet Package Manager Console:

foreach($project in $projects){ $packages = Get-Package -ProjectName $project.Name; foreach($package in $packages){ Uninstall-Package -ProjectName $project.Name -Id $package.Id -Force} }
Michal Steyn
  • 341
  • 2
  • 5
  • 4
    Great answer. Script for a single project: `$packages = Get-Package -ProjectName MyProjectName; foreach($package in $packages){ Uninstall-Package -ProjectName MyProjectName -Id $package.Id -Force}` – Mohsen Afshin Dec 03 '16 at 09:05
10

Using the -Force parameter in my case left project file modifications and references to some binaries that should have been removed when normally uninstalling the packages.

Here is a naive method to uninstall all packages from specific projects without using the -Force parameter. Effectively it tries to uninstall the packages over and over again until there are no packages left, so you will see some errors mentioning dependent packages (if you have them) but they will turn up less and less as the leaf packages get removed each iteration.

Also worth mentioning I've only tested the following PowerShell snippets in the PackageManager console. ("Tools > NuGet Package Manager > Package Manager Console")

Uninstall all the packages from all the projects in a solution

while((Get-Project -All | Get-Package).Length -gt 0) { Get-Project -All | Get-Package | Uninstall-Package }

Only remove Projects containing the word "WildCardSearch"

while((Get-Project -All | Where-Object ProjectName -like '*WildCardSearch*'  | Get-Package).Length -gt 0) { Get-Project -All | Where-Object ProjectName -like '*WildCardSearch*' | Get-Package | Uninstall-Package }

Note that if you have another issue apart from dependent packages preventing an uninstall of the package this snippet will run forever until you manually stop it.

Nathan
  • 633
  • 7
  • 12
4

I do not believe this is possible so un-install ALL packages at once. However, as you already indicated you can un-install a package, but you can also tell it to un install its dependencies doing the following:

Uninstall-Package OpenIdPortableArea –RemoveDependencies

Here is a blog by Marcus Hammarberg explaining this: http://www.marcusoft.net/2011/02/nuget-uninstall-remove-dependencies.html

TResponse
  • 3,940
  • 7
  • 43
  • 63
  • 3
    "Remove dependencies" works when 1 package depends on many others. It doesn't work when N packages depend on the core one (e.g. Boost). You need to remove the NuGet Boost packages manually one by one, which is a pain. – quant_dev Dec 19 '15 at 13:57
3

Auto-restoring (uninstall and install without updating to the latest version) of packages using Package Manager Console:

This runs for the whole solution:

Update-Package -Reinstall

... or for specific project only:

Update-Package -Reinstall -Project [ProjectName]

Mariusz
  • 908
  • 1
  • 13
  • 28
  • Well, it woulda been nice to know the first command runs against your entire solution, regardless of what project you have set as default in the package manager console window. I never even looked at the second command until writing this message, ugh! – Post Impatica Sep 20 '22 at 22:58
  • Updated the answer to point that out @PostImpatica :-) – Mariusz Sep 21 '22 at 09:04
1

Updated a script to remove all nuget packages for single project in VS solution:

$projectName = "MyProjectName"; $packages = Get-Package -ProjectName $projectName; foreach($package in $packages){ Uninstall-Package -ProjectName $projectName -Id $package.Id -Force}
Vlad Setchin
  • 121
  • 1
  • 5
0

Dummy old-school for loop:

$packages = get-package
$packageId = "Apache.NMS.ActiveMQ"


$counter = 1
foreach($package in $packages){     
    if($package.Id -eq $packageId)
    {   
        Write-Host "`n$counter-Deleting Package:`n"
        $package        
        Uninstall-Package -Id $packageId -ProjectName $package.ProjectName -RemoveDependencies 
        Write-Host "`n============================================`n"
        $counter++
    }
}
Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158