如何删除 .NET Core 运行时和 SDKHow to remove the .NET Core Runtime and SDK

经过一段时间后,在安装 .NET Core 运行时和 SDK 的更新版本时,用户可能需要从计算机中删除过时的 .NET Core 版本。Over time, as you install updated versions of the .NET Core runtime and SDK, you may want to remove outdated versions of .NET Core from your machine. 如有关 .NET Core 版本选择的文章中详述,删除旧版运行时可能会更改为运行共享框架应用程序所选择的运行时。Removing older versions of the runtime may change the runtime chosen to run shared framework applications, as detailed in the article on .NET Core version selection.

是否应删除某个版本?Should I remove a version?

借助 .NET Core 版本选择行为和 .NET Core 各个更新之间的运行时兼容性,可安全地删除以前的版本。The .NET Core version selection behaviors and the runtime compatibility of .NET Core across updates enables safe removal of previous versions. .NET Core 运行时更新在主版本“区段”(如 1.x 和 2.x)中兼容。.NET Core runtime updates are compatible within a major version 'band' such as 1.x and 2.x. 此外,较新版本的 .NET Core SDK 通常能够以兼容的方式生成以运行时的早期版本为目标的应用程序。Additionally, newer releases of the .NET Core SDK generally maintain the ability to build applications that target previous versions of the runtime in a compatible manner.

通常,只需要应用程序所需的最新 SDK 和运行时的最新补丁版本。In general, you only need the latest SDK and latest patch version of the runtimes required for your application. 需要保留旧版 SDK 或运行时版本的实例包括维护基于 project.json 的应用程序 。Instances where you might want to keep older SDK or runtime versions include maintaining project.json-based applications. 除非应用程序有需保留早期 SDK 或运行时的特定原因,否则可以安全地删除旧版本。Unless your application has specific reasons for earlier SDKs or runtimes, you may safely remove older versions.

确定安装内容Determine what is installed

从 .NET Core 2.1 开始,.NET CLI 提供一些可用于列出计算机上安装的 SDK 和运行时版本的选项。Starting with .NET Core 2.1, the .NET CLI has options you can use to list the versions of the SDK and runtime that are installed on your machine. 使用 dotnet --list-sdks 查看计算机上安装的 SDK 列表。Use dotnet --list-sdks to see the list of SDKs installed on your machine. 使用 dotnet --list-runtimes 查看计算机上安装的运行时列表。Use dotnet --list-runtimes to see the list of runtimes installed on your machine. 有关详细信息,请参阅如何检查是否已安装 .NET CoreFor more information, see How to check that .NET Core is already installed.

卸载 .NET CoreUninstall .NET Core

.NET Core 使用 Windows“应用和功能”对话框来删除 .NET Core 运行时和 SDK 的版本 。.NET Core uses the Windows Apps & features dialog to remove versions of the .NET Core runtime and SDK. 下图显示了“应用和功能”对话框 。The following figure shows the Apps & features dialog. 可以搜索“core sdk”来筛选和显示安装的 .NET Core 版本 。You can search for core sdk to filter and show installed versions of .NET Core.

用来删除 .NET Core 的“添加/删除程序”

选择要从计算机中删除的任何版本,然后单击“卸载” 。Select any versions you want to remove from your machine and click Uninstall.

Linux 还提供其他可用来卸载 .NET Core(SDK 或运行时)的选项。There are more options to uninstall .NET Core (either SDK or runtime) on Linux. 卸载 .NET Core 的最佳方法是镜像用来安装 .NET Core 的操作。The best way for you to uninstall .NET Core is to mirror the action you used to install .NET Core. 具体取决于所选择的分发和安装方法。The specifics depend on your chosen distribution and the installation method.

重要

有关 Red Hat 安装,请参阅 Red Hat 入门指南,了解有关安装和卸载 .NET Core 的信息。For Red Hat installations, consult the Red Hat Getting Started Guide for information on installing and uninstalling .NET Core.

从 .NET Core 2.1 开始,使用包管理器升级时无需卸载 .NET Core SDK。Starting with .NET Core 2.1, there's no need to uninstall the .NET Core SDK when upgrading it using a package manager. 包管理器 updaterefresh 命令将在成功安装较新版本后自动删除旧版本。The package manager update or refresh commands will automatically remove the older version upon the successful installation of a newer version.

如果使用包管理器安装了 .NET Core,则使用同一包管理器卸载 .NET SDK 或运行时。If you installed .NET Core using a package manager, you use that same package manager to uninstall .NET SDK or runtime. .NET Core 安装支持最常用的包管理器。.NET Core installations support most popular package managers. 有关环境中的精确语法,请查阅分发的包管理器文档:Consult the documentation for your distribution's package manager for the precise syntax in your environment:

  • apt-get(8) 由基于 Debian 的系统(包括 Ubuntu)使用。apt-get(8) is used by Debian based systems, including Ubuntu.
  • yum(8) 用于 Fedora、CentOS 和 Oracle Linux。yum(8) is used on Fedora, CentOS, and Oracle Linux.
  • zypper(8) 用于 openSUSE 和 SUSE Linux Enterprise System (SLES)。zypper(8) is used on openSUSE and SUSE Linux Enterprise System (SLES).
  • dnf(8) 用于 Fedora。dnf(8) is used on Fedora.

几乎在所有情况下,删除包的命令都是 removeIn almost all cases, the command to remove a package is remove.

大多数包管理器的 .NET Core SDK 安装包名称为 dotnet-sdk,后跟版本号。The package name for the .NET Core SDK installation for most package managers is dotnet-sdk, followed by the version number. 从 .NET Core SDK 2.1.300 版和运行时的 2.1 版开始,只需要主版本号和次版本号:例如,可将 .NET Core SDK 2.1.300 版引用为包 dotnet-sdk-2.1Starting with the version 2.1.300 of the .NET Core SDK and version 2.1 of the runtime, only the major and minor version numbers are necessary: for example, the .NET Core SDK version 2.1.300 can be referenced as the package dotnet-sdk-2.1. 以前的版本则需要整个版本字符串:例如,.NET Core SDK 2.1.200 版需要 dotnet-sdk-2.1.200Prior versions require the entire version string: for example, dotnet-sdk-2.1.200 would be required for version 2.1.200 of the .NET Core SDK.

对于仅安装了运行时而未安装 SDK 的计算机,.NET Core 运行时的包名称为 dotnet-runtime-<version>,整个运行时堆栈的包名称为 aspnetcore-runtime-<version>For machines that have installed only the runtime, and not the SDK, the package name is dotnet-runtime-<version> for the .NET Core runtime, and aspnetcore-runtime-<version> for the entire runtime stack.

使用包管理器卸载 SDK 时,2.0 之前的 .NET Core 安装不会卸载主机应用程序。.NET Core installations earlier than 2.0 didn't uninstall the host application when the SDK was uninstalled using the package manager. 使用 apt-get,该命令为:Using apt-get, the command is:

apt-get remove dotnet-host

请注意,没有版本附加到 dotnet-hostNote that there's no version attached to dotnet-host.

如果使用 tarball 安装,则必须使用手动方法删除 .NET Core。If you installed using a tarball, you must remove .NET Core using the manual method.

在 Linux 上,必须通过删除进行版本控制的目录,分别删除 SDK 和运行时。On Linux, you must remove the SDKs and runtimes separately, by removing the versioned directories. 删除它们会从磁盘中删除 SDK 和运行时。Removing them deletes the SDK and runtime from disk. 例如,要删除 1.0.1 SDK 和运行时,可使用以下 bash 命令:For example, to remove the 1.0.1 SDK and runtime, you would use the following bash commands:

version="1.0.1"
sudo rm -rf /usr/local/share/dotnet/sdk/$version
sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.NETCore.App/$version
sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.AspNetCore.All/$version
sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.AspNetCore.App/$version
sudo rm -rf /usr/local/share/dotnet/host/fxr/$version

SDK 和运行时的父目录列在 dotnet --list-sdksdotnet --list-runtimes 命令的输出中,如上表所示。The parent directories for the SDK and runtime are listed in the output from the dotnet --list-sdks and dotnet --list-runtimes command, as shown in the earlier table.

在 Mac 上,必须通过删除进行版本控制的目录,分别删除 SDK 和运行时。On Mac, you must remove the SDKs and runtimes separately, by removing the versioned directories. 删除它们会从磁盘中删除 SDK 和运行时。Removing them deletes the SDK and runtime from disk. 例如,要删除 1.0.1 SDK 和运行时,可使用以下 bash 命令:For example, to remove the 1.0.1 SDK and runtime, you would use the following bash commands:

version="1.0.1"
sudo rm -rf /usr/local/share/dotnet/sdk/$version
sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.NETCore.App/$version
sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.AspNetCore.All/$version
sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.AspNetCore.App/$version
sudo rm -rf /usr/local/share/dotnet/host/fxr/$version

SDK 和运行时的父目录列在 dotnet --list-sdksdotnet --list-runtimes 命令的输出中,如上表所示。The parent directories for the SDK and runtime are listed in the output from the dotnet --list-sdks and dotnet --list-runtimes command, as shown in the earlier table.

.NET Core 卸载工具.NET Core Uninstall Tool

.NET Core 卸载工具 (dotnet-core-uninstall) 使你可以从系统中删除 .NET Core SDK 和运行时。The .NET Core Uninstall Tool (dotnet-core-uninstall) lets you remove .NET Core SDKs and runtimes from a system. 可使用选项集合来指定应卸载的版本。A collection of options is available to specify which versions should be uninstalled.

.NET Core SDK 版本的 Visual Studio 依赖项Visual Studio dependency on .NET Core SDK versions

在 Visual Studio 2019 版本 16.3 之前,Visual Studio 安装程序称为独立的 .NET Core SDK 安装程序。Before Visual Studio 2019 version 16.3, Visual Studio installers called the standalone .NET Core SDK installer. 因此,SDK 版本显示在 Windows“应用和功能”对话框中 。As a result, the SDK versions appear in the Windows Apps & features dialog. 使用独立安装程序删除 Visual Studio 安装的 .NET Core SDK 可能会破坏 Visual Studio。Removing .NET Core SDKs that were installed by Visual Studio using the standalone installer may break Visual Studio. 如果 Visual Studio 在卸载 SDK 之后出现问题,请在该特定版本的 Visual Studio 上运行修复。If Visual Studio has problems after you uninstall SDKs, run Repair on that specific version of Visual Studio. 下表显示了 .NET Core SDK 版本的一些 Visual Studio 依赖项:The following table shows some of the Visual Studio dependencies on .NET Core SDK versions:

Visual Studio 版本Visual Studio version .NET Core SDK 版本.NET Core SDK version
Visual Studio 2019 版本 16.2Visual Studio 2019 version 16.2 .NET Core SDK 2.2.4xx、2.1.8xx.NET Core SDK 2.2.4xx, 2.1.8xx
Visual Studio 2019 版本 16.1Visual Studio 2019 version 16.1 .NET Core SDK 2.2.3xx、2.1.7xx.NET Core SDK 2.2.3xx, 2.1.7xx
Visual Studio 2019 版本 16.0Visual Studio 2019 version 16.0 .NET Core SDK 2.2.2xx、2.1.6xx.NET Core SDK 2.2.2xx, 2.1.6xx
Visual Studio 2017 版本 15.9Visual Studio 2017 version 15.9 .NET Core SDK 2.2.1xx、2.1.5xx.NET Core SDK 2.2.1xx, 2.1.5xx
Visual Studio 2017 版本 15.8Visual Studio 2017 version 15.8 .NET Core SDK 2.1.4xx.NET Core SDK 2.1.4xx

从 Visual Studio 2019 版本 16.3 开始,Visual Studio 负责其自己的 .NET Core SDK 副本。Starting with Visual Studio 2019 version 16.3, Visual Studio is in charge of its own copy of the .NET Core SDK. 因此,“应用和功能”对话框中将不再显示这些 SDK 版本 。For that reason, you no longer see those SDK versions in the Apps & features dialog.

删除 NuGet 回退文件夹Remove the NuGet fallback folder

在 .NET Core 3.0 SDK 之前,.NET Core SDK 安装程序使用名为 NuGetFallbackFolder 的文件夹存储 NuGet 包的缓存 。Before .NET Core 3.0 SDK, the .NET Core SDK installers used a folder named NuGetFallbackFolder to store a cache of NuGet packages. 此缓存在操作期间(如 dotnet restoredotnet build /t:Restore)使用。This cache was used during operations such as dotnet restore or dotnet build /t:Restore. NuGetFallbackFolder 在 Windows 上位于 C:\Program Files\dotnet\sdk,在 macOS 上位于 /usr/local/share/dotnet/sdk 。The NuGetFallbackFolder is located at C:\Program Files\dotnet\sdk on Windows and at /usr/local/share/dotnet/sdk on macOS.

如果是以下情况,则可能需要删除此文件夹:You may want to remove this folder, if:

  • 仅使用 .NET Core 3.0 SDK 或更高版本进行开发。You're only developing using .NET Core 3.0 SDK or later versions.
  • 你使用早于 3.0 的 .NET Core SDK 版本进行开发,但可以联机工作。You're developing using .NET Core SDK versions earlier than 3.0, but you can work online.

如果要删除 NuGet 回退文件夹,可以将其删除,但需要管理员权限才能执行此操作。If you want to remove the NuGet fallback folder, you can delete it, but you'll need admin privileges to do so.

建议不要删除 dotnet 文件夹。It's not recommended to delete the dotnet folder. 这样做会删除以前安装的所有全局工具。Doing so would remove any global tools you've previously installed. 此外,在 Windows 上:Also, on Windows:

  • 你将中断 Visual Studio 2019 版本 16.3 及更高版本。You'll break Visual Studio 2019 version 16.3 and later versions. 可以运行“修复” 来恢复。You can run Repair to recover.
  • 如果“应用和功能”对话框中存在 .NET Core SDK 条目,它们将是孤立的 。If there are .NET Core SDK entries in the Apps & features dialog, they'll be orphaned.