Remove old .NET versions with the new uninstall tool
- 9 minutes read - 1865 wordsMicrosoft have released a tool to more easily uninstall old .NET SDKs and Runtime’s from your machine. I use a Mac for my development and it’s always been annoying that you couldn’t easily remove old .NET versions, so I thought I’d give it a try and let you know how it went.
Installing the tool
Firstly, you need to install the uninstaller… that’s right. Currently the tool is released as a binary on GitHub because they were having problems releasing it as a .NET Tool. The GitHub releases shows how to install the tool:
- Download the tarball
- With a terminal, go into the directly of the tarball
- Run the following commands to install the tool:
$ mkdir -p ~/dotnet-core-uninstall
$ tar -zxf dotnet-core-uninstall.tar.gz -C ~/dotnet-core-uninstall
$ cd ~/dotnet-core-uninstall
$ ./dotnet-core-uninstall -h
which displays the following output:
dotnet-core-uninstall:
Remove specified .NET Core SDKs or Runtimes. By default, this tool does not
uninstall versions that might be needed for Visual Studio for Mac or SDKs.
Read the documentation for the .NET Core Uninstall Tool at
https://aka.ms/dotnet-core-uninstall.
Usage:
dotnet-core-uninstall [options] [command]
Options:
--version Display version information
Commands:
list List .NET Core SDKs or Runtimes that can be
removed with this tool.
whatif, dry-run <VERSION> Display .NET Core SDKs and Runtimes that will
be removed.
remove <VERSION> Remove the specified .NET Core SDKs or
Runtimes.
From here, it looks like I’m on my own because I need to decide what I want to do. As a starting point, let’s check out what I’ve got installed on my machine.
Listing SDKs and Runtimes
Based on the help output, I should be able to run the following command to list the SDKs and Runtimes that can be uninstalled:
$ ./dotnet-core-uninstall list
This tool can not uninstall versions of the runtime or SDK that are installed using zip/scripts. The versions that can be uninstalled with this tool are:
.NET Core SDKs:
3.1.102 (x64) [Used by Visual Studio for Mac. Specify individually or use —-force toremove]
3.1.101 (x64)
3.1.100-preview3-014645 (x64)
3.1.100-preview2-014569 (x64)
3.1.100-preview1-014459 (x64)
3.1.100 (x64)
3.0.100-rc1-014190 (x64)
3.0.100 (x64)
2.2.402 (x64)
2.2.203 (x64)
.NET Core Runtimes:
3.1.2 (x64) [Used by Visual Studio for Mac or SDKs. Specify individually or use —-force to remove]
3.1.1 (x64)
3.1.0-preview3.19555.2 (x64)
3.1.0-preview3.19553.2 (x64)
3.1.0-preview2.19528.8 (x64)
3.1.0-preview2.19525.6 (x64)
3.1.0-preview1.19508.20 (x64)
3.1.0-preview1.19506.1 (x64)
3.1.0 (x64)
3.0.0-rc1.19457.4 (x64)
3.0.0-rc1-19456-20 (x64)
3.0.0 (x64) [Used by Visual Studio for Mac or SDKs. Specify individually or use —-force to remove]
2.2.7 (x64) [Used by Visual Studio for Mac or SDKs. Specify individually or use —-force to remove]
2.2.4 (x64)
So I’ve got quite a few SDKs and Runtimes. I usually develop from the latest and greatest with VS Code/.NET CLI so my plan is to remove everything except 3.1.102 SDK and 3.1.2 runtime.
Removing SDKs and Runtimes
I know that I want to remove everything except 3.1.102/3.1.2, but how do I do that? Let’s check out the help option for the remove command:
$ ./dotnet-core-uninstall remove -h
remove:
Remove the specified .NET Core SDKs or Runtimes.
Usage:
dotnet-core-uninstall remove [options] [<VERSION>...]
Arguments:
<VERSION> The specified version to uninstall. You may list several versions. Response files are supported.
Options:
--all Remove all .NET Core SDKs or Runtimes. (*)
--all-below <VERSION> Remove .NET Core SDKs or Runtimes below the specified version. The specified version will remain. (*)
--all-but <VERSIONS> Remove .NET Core SDKs or Runtimes, except those specified. (*)
--all-but-latest Remove .NET Core SDKs or Runtimes, except the one highest version. (*)
--all-lower-patches Remove .NET Core SDKs or Runtimes superseded by higher patches. Protects global.json. (*)
--all-previews Remove .NET Core SDKs or Runtimes marked as previews. (*)
--all-previews-but-latest Remove .NET Core SDKs or Runtimes marked as previews, except the one highest preview. (*)
--force Force removal of versions that might be used by Visual Studio for Mac or SDKs.
--major-minor <MAJOR_MINOR> Remove .NET Core SDKs or Runtimes that match the specified `major.minor` version.
--runtime Remove .NET Core Runtimes only.
--sdk Remove .NET Core SDKs only.
-v, --verbosity <LEVEL> Set the verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic].
-y, --yes Execute the command without requiring Y/n confirmation.
(*) By default, SDKs and Runtimes that have a high probability of being used by Visual Studio for Mac or SDKs are not removed. To remove these, specify them individually or use --force. If removing SDKs or Runtimes causes issues with your installation of Visual Studio for Mac, run “Repair”. SDKs and Runtimes are available for download at aka.ms/dotnet-core-download.
Ok, it looks like I want to run --sdk --all-below 3.1.102 --force
and --runtime --all-below 3.1.2 --force
, which should remove everything below 3.1.102/3.1.2 including things that Visual Studio for Mac might use. But’s that’s fine, since I don’t use Visual Studio for Mac.
Dry run
Before blowing up my Mac, let’s do a ‘dry run’ to see if the command I have formulated will do what I think it will:
SDKs:
$ ./dotnet-core-uninstall dry-run --sdk --all-below 3.1.102 --force
*** DRY RUN OUTPUT
Specified versions:
Microsoft .NET Core SDK 3.1.101 (x64)
Microsoft .NET Core SDK 3.1.100-preview3-014645 (x64)
Microsoft .NET Core SDK 3.1.100-preview2-014569 (x64)
Microsoft .NET Core SDK 3.1.100-preview1-014459 (x64)
Microsoft .NET Core SDK 3.1.100 (x64)
Microsoft .NET Core SDK 3.0.100-rc1-014190 (x64)
Microsoft .NET Core SDK 3.0.100 (x64)
Microsoft .NET Core SDK 2.2.402 (x64)
Microsoft .NET Core SDK 2.2.203 (x64)
*** END DRY RUN OUTPUT
To avoid breaking Visual Studio for Mac or other problems, read https://aka.ms/dotnet-core-uninstall.
Run as administrator and use the remove command to uninstall these items.
Runtimes:
$ ./dotnet-core-uninstall dry-run --runtime --all-below 3.1.2 --force
*** DRY RUN OUTPUT
Specified versions:
Microsoft .NET Core Runtime 3.1.1 (x64)
Microsoft .NET Core Runtime 3.1.0-preview3.19555.2 (x64)
Microsoft .NET Core Runtime 3.1.0-preview3.19553.2 (x64)
Microsoft .NET Core Runtime 3.1.0-preview2.19528.8 (x64)
Microsoft .NET Core Runtime 3.1.0-preview2.19525.6 (x64)
Microsoft .NET Core Runtime 3.1.0-preview1.19508.20 (x64)
Microsoft .NET Core Runtime 3.1.0-preview1.19506.1 (x64)
Microsoft .NET Core Runtime 3.1.0 (x64)
Microsoft .NET Core Runtime 3.0.0-rc1.19457.4 (x64)
Microsoft .NET Core Runtime 3.0.0-rc1-19456-20 (x64)
Microsoft .NET Core Runtime 3.0.0 (x64)
Microsoft .NET Core Runtime 2.2.7 (x64)
Microsoft .NET Core Runtime 2.2.4 (x64)
*** END DRY RUN OUTPUT
To avoid breaking Visual Studio for Mac or other problems, read https://aka.ms/dotnet-core-uninstall.
Run as administrator and use the remove command to uninstall these items.
Warning: Microsoft .NET Core Runtime 3.0.0 (x64): Used by Visual Studio for Mac or SDKs. Specify individually or use —-force to remove
Uninstalling this item will cause Visual Studio for to break.
Warning: Microsoft .NET Core Runtime 2.2.7 (x64): Used by Visual Studio for Mac or SDKs. Specify individually or use —-force to remove
Uninstalling this item will cause Visual Studio for to break.
Looks good, let’s go for it!
Proper run
Let’s remove the SDKs and Runtimes for real now. Note, you have to run the commands in elevated privileges' (sudo).
SDKs:
$ sudo ./dotnet-core-uninstall remove --sdk --all-below 3.1.102 --force
Password:
The following items will be removed:
Microsoft .NET Core SDK 3.1.101 (x64)
Microsoft .NET Core SDK 3.1.100-preview3-014645 (x64)
Microsoft .NET Core SDK 3.1.100-preview2-014569 (x64)
Microsoft .NET Core SDK 3.1.100-preview1-014459 (x64)
Microsoft .NET Core SDK 3.1.100 (x64)
Microsoft .NET Core SDK 3.0.100-rc1-014190 (x64)
Microsoft .NET Core SDK 3.0.100 (x64)
Microsoft .NET Core SDK 2.2.402 (x64)
Microsoft .NET Core SDK 2.2.203 (x64)
To avoid breaking Visual Studio for Mac or other problems, read https://aka.ms/dotnet-core-uninstall.
Do you want to continue? [Y/n] Y
Uninstalling: Microsoft .NET Core SDK 3.1.101 (x64).
Uninstalling: Microsoft .NET Core SDK 3.1.100-preview3-014645 (x64).
Uninstalling: Microsoft .NET Core SDK 3.1.100-preview2-014569 (x64).
Uninstalling: Microsoft .NET Core SDK 3.1.100-preview1-014459 (x64).
Uninstalling: Microsoft .NET Core SDK 3.1.100 (x64).
Uninstalling: Microsoft .NET Core SDK 3.0.100-rc1-014190 (x64).
Uninstalling: Microsoft .NET Core SDK 3.0.100 (x64).
Uninstalling: Microsoft .NET Core SDK 2.2.402 (x64).
Uninstalling: Microsoft .NET Core SDK 2.2.203 (x64).
Runtimes:
$ sudo ./dotnet-core-uninstall remove --runtime --all-below 3.1.2 --force
The following items will be removed:
Microsoft .NET Core Runtime 3.1.1 (x64)
Microsoft .NET Core Runtime 3.1.0-preview3.19555.2 (x64)
Microsoft .NET Core Runtime 3.1.0-preview3.19553.2 (x64)
Microsoft .NET Core Runtime 3.1.0-preview2.19528.8 (x64)
Microsoft .NET Core Runtime 3.1.0-preview2.19525.6 (x64)
Microsoft .NET Core Runtime 3.1.0-preview1.19508.20 (x64)
Microsoft .NET Core Runtime 3.1.0-preview1.19506.1 (x64)
Microsoft .NET Core Runtime 3.1.0 (x64)
Microsoft .NET Core Runtime 3.0.0-rc1.19457.4 (x64)
Microsoft .NET Core Runtime 3.0.0-rc1-19456-20 (x64)
Microsoft .NET Core Runtime 3.0.0 (x64)
Microsoft .NET Core Runtime 2.2.7 (x64)
Microsoft .NET Core Runtime 2.2.4 (x64)
To avoid breaking Visual Studio for Mac or other problems, read https://aka.ms/dotnet-core-uninstall.
Do you want to continue? [Y/n] Y
Microsoft .NET Core Runtime 3.0.0 (x64): Used by Visual Studio for Mac or SDKs. Specify individually or use —-force to remove
Uninstalling this item will cause Visual Studio for Mac to break.
Are you sure you want to continue? [Y/n] Y
Microsoft .NET Core Runtime 2.2.7 (x64): Used by Visual Studio for Mac or SDKs. Specify individually or use —-force to remove
Uninstalling this item will cause Visual Studio for Mac to break.
Are you sure you want to continue? [Y/n] Y
Uninstalling: Microsoft .NET Core Runtime 3.1.1 (x64).
Uninstalling: Microsoft .NET Core Runtime 3.1.0-preview3.19555.2 (x64).
Uninstalling: Microsoft .NET Core Runtime 3.1.0-preview3.19553.2 (x64).
Uninstalling: Microsoft .NET Core Runtime 3.1.0-preview2.19528.8 (x64).
Uninstalling: Microsoft .NET Core Runtime 3.1.0-preview2.19525.6 (x64).
Uninstalling: Microsoft .NET Core Runtime 3.1.0-preview1.19508.20 (x64).
Uninstalling: Microsoft .NET Core Runtime 3.1.0-preview1.19506.1 (x64).
Uninstalling: Microsoft .NET Core Runtime 3.1.0 (x64).
Uninstalling: Microsoft .NET Core Runtime 3.0.0-rc1.19457.4 (x64).
Uninstalling: Microsoft .NET Core Runtime 3.0.0-rc1-19456-20 (x64).
Uninstalling: Microsoft .NET Core Runtime 3.0.0 (x64).
Uninstalling: Microsoft .NET Core Runtime 2.2.7 (x64).
Uninstalling: Microsoft .NET Core Runtime 2.2.4 (x64).
Ok, it looks like my plan worked. Let’s check the SDKs/Runtimes still installed to confirm it worked.
Confirm removal
We can list the current SDKs/Runtimes with the following command:
$ ./dotnet-core-uninstall list
This tool can not uninstall versions of the runtime or SDK that are installed using zip/scripts. The versions that can be uninstalled with this tool are:
.NET Core SDKs:
3.1.102 (x64) [Used by Visual Studio for Mac. Specify individually or use —-force to remove]
.NET Core Runtimes:
3.1.2 (x64) [Used by Visual Studio for Mac or SDKs. Specify individually or use —-force to remove]
and also with:
$ dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 3.1.102
Commit: 573d158fea
Runtime Environment:
OS Name: Mac OS X
OS Version: 10.14
OS Platform: Darwin
RID: osx.10.14-x64
Base Path: /usr/local/share/dotnet/sdk/3.1.102/
Host (useful for support):
Version: 3.1.2
Commit: 916b5cba26
.NET Core SDKs installed:
3.1.102 [/usr/local/share/dotnet/sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.App 3.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download
Great, they both agree and I only have the latest SDK/Runtime on my system!
Summary
Microsoft have released a command line tool to remove old SDKs/Runtimes from your system. It has many options allowing you to keep the latest version, remove everything except those versions used by Visual Studio and remove only previews, among other options. It was quick and painless to install and run the tool. You can also do dry runs to confirm your removal command before actually running it. I would recommend this tool to any Mac users that use the .NET CLI.
In the future, it would be nice if this was a .NET Tool such that it would be easier to install. I think we might see this in the next couple of iterations of this tool. But for now, this works pretty well.