本文档介绍了如何删除 Compute Engine 实例。如需详细了解实例的生命周期,请参阅 Compute Engine 实例生命周期。
如果您不再需要实例,请将其删除,以免实例及其附加资源继续产生费用。
准备工作
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 Google Cloud 服务和 API 的过程。如需从本地开发环境运行代码或示例,您可以通过选择以下选项之一向 Compute Engine 进行身份验证:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
- Set a default region and zone.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
-
针对实例的
compute.instances.delete
权限 -
如需强制删除挂接的磁盘:针对磁盘的
compute.disks.delete
权限 如果您删除在单租户节点上托管的实例,则您将继续为单租户节点本身支付费用,而不是为在该节点上托管的各个实例支付费用。
如果您有承诺使用折扣,则无论您是否使用这些资源,您都需要继续为所承诺的资源付费。
如果您保留了挂接到实例的任何资源,这些资源会继续产生费用,直到您将其删除。例如,如果您删除实例但保留了挂接到该实例的磁盘,则这些磁盘会继续产生费用。
如需保留挂接到 N1 虚拟机 (VM) 实例的 GPU,请从虚拟机中移除 GPU。
如需保留已连接的磁盘,请执行以下操作之一:
如需保留启动磁盘,请将启动磁盘与实例分离。
如需保留启动磁盘或非启动磁盘,请将磁盘的自动删除设置设置为
false
。
如需保留本地 SSD 磁盘的数据,请将该磁盘的数据复制到持久性存储选项。
如需重复使用实例的静态外部 IP 地址,请取消分配 IP 地址。
-
此选项会删除实例,并删除或强制删除启动盘、非启动盘或所有已附加的磁盘。
-
此选项会删除实例,但会保留启动磁盘、非启动磁盘或所有已挂接磁盘以供日后使用。
如果您将已附加的磁盘配置为在删除实例时保留,则可以使用 Google Cloud CLI 覆盖此设置并强制删除该磁盘。
如果您已在实例中启用正常关闭,则可以删除实例,而无需正常关闭实例,也可以使用 Google Cloud 控制台、gcloud CLI 或 REST API 结束正在进行的正常关闭。
如需同时删除多个实例,请使用 Google Cloud 控制台或 gcloud CLI(对于位于同一可用区中的实例)。
在 Google Cloud 控制台中,前往虚拟机实例页面。
选择要删除的实例。
点击
删除。在对话框中,执行以下操作:
可选:如需在不优雅关闭实例的情况下删除实例,或结束正在进行的优雅关闭,请选中跳过优雅关闭(如果适用)复选框。
请点击删除来确认操作。
INSTANCE_NAMES
:以空格分隔的实例名称列表,例如instance-01 instance-02 instance-03
。ZONE
:实例所在的可用区。如需强制删除挂接到一个或多个实例的磁盘,请添加
--delete-disks
标志:gcloud compute instances delete INSTANCE_NAMES \ --delete-disks=DELETE_DISK_TYPE \ --zone=ZONE
将
DELETE_DISK_TYPE
替换为以下某个值:如需删除已挂接的启动永久性存储空间和非启动永久性存储空间,请执行以下操作:
all
如需仅删除已附加的启动永久性存储:
boot
如需仅删除非启动永久性存储,请执行以下操作:
data
如果您已在一个或多个实例中启用正常关闭功能,则可以删除这些实例,而无需正常关闭它们,也可以手动结束正在进行的正常关闭。为此,请使用带有
--no-graceful-shutdown
标志的gcloud beta compute instances delete
命令:gcloud beta compute instances delete INSTANCE_NAMES \ --no-graceful-shutdown \ --zone=ZONE
PROJECT_ID
:实例所在项目的 ID。ZONE
:实例的可用区。INSTANCE_NAME
:实例名称。INSTANCE_NAMES
:以空格分隔的实例名称列表,例如instance-01 instance-02 instance-03
。KEEP_DISK_TYPE
:指定以下某个值:如需保留已附加的启动和非启动永久性存储空间,请执行以下操作:
all
如需仅保留已附加的启动永久性存储空间:
boot
如需仅保留已附加的非启动永久性存储空间:
data
ZONE
:实例所在的可用区。
C#
如需在本地开发环境中使用本页面上的 .NET 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Go
如需在本地开发环境中使用本页面上的 Go 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Java
如需在本地开发环境中使用本页面上的 Java 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Node.js
如需在本地开发环境中使用本页面上的 Node.js 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
PHP
如需在本地开发环境中使用本页面上的 PHP 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
如需了解详情,请参阅 Set up authentication for a local development environment。
Python
如需在本地开发环境中使用本页面上的 Python 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Ruby
如需在本地开发环境中使用本页面上的 Ruby 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭据。
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。
所需的角色
如需获得删除计算实例所需的权限,请让管理员向您授予项目的 Compute Instance Admin (v1) (
roles/compute.instanceAdmin.v1
) IAM 角色。 如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。此预定义角色包含删除计算实例所需的权限。如需查看所需的确切权限,请展开所需权限部分:
所需权限
您需要具备以下权限才能删除计算实例:
结算影响
删除计算实例后,除非符合以下任何情况,否则您无需再为该实例及其挂接的资源付费:
如需了解详情,请参阅虚拟机实例价格。
保留附加资源
在某些情况下,您可能希望在删除计算实例之前保留其附加的某个资源。您可以通过以下方式保留已附加的资源:
删除实例
当您删除计算实例时,Compute Engine 会先停止实例,然后再将其删除。
如果您同时删除一个或多个实例,则必须决定如何处理已附加的磁盘:
删除实例和所有附加的资源
根据您在删除计算实例时要执行的操作,使用以下选项:
如需删除一个或多个实例以及所有关联的资源,请选择以下选项之一:
控制台
gcloud
如需删除同一可用区中的一个或多个实例,请使用
gcloud compute instances delete
命令:gcloud compute instances delete INSTANCE_NAMES \ --zone=ZONE
替换以下内容:
您可以选择执行以下一项或两项操作:
C#
Go
Java
Node.js
PHP
Python
Ruby
REST
如需删除实例,请向
instances delete
方法发出DELETE
请求:DELETE https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME
替换以下内容:
(可选)如果您已在实例中启用正常关闭功能,则可以删除实例而不正常关闭,也可以手动结束正在进行的正常关闭。为此,请向 Beta 版
instances.delete
方法发出DELETE
请求。在请求网址中添加noGracefulShutdown
查询参数并设置为true
:DELETE https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME?noGracefulShutdown=true
删除实例并保留磁盘
默认情况下,删除计算实例会删除该实例及其附加的资源。但是,使用 gcloud CLI 删除实例时,您可以指定保留挂接的磁盘,而不考虑磁盘的自动删除设置。
如需删除同一可用区中的一个或多个实例,同时保留其挂接的磁盘,请使用带有
--keep-disks
标志的gcloud compute instances delete
命令:gcloud compute instances delete INSTANCE_NAMES \ --keep-disks=KEEP_DISK_TYPE \ --zone=ZONE
替换以下内容:
(可选)如果您在一个或多个实例中启用了正常关闭,则可以删除这些实例,而无需正常关闭它们,也可以手动结束正在进行的正常关闭。为此,请使用带有
--no-graceful-shutdown
标志的gcloud beta compute instances delete
命令:gcloud beta compute instances delete VM_NAMES \ --keep-disks=KEEP_DISK_TYPE \ --no-graceful-shutdown \ --zone=ZONE
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-04。
-