本文档介绍了如何删除 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
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
- 针对实例的
compute.instances.delete
权限 -
如需强制删除挂接的磁盘:针对磁盘的
compute.disks.delete
权限 如果您删除在单租户节点上托管的实例,则您仍需为单租户节点支付费用。
如果您删除正在使用预留的实例,则需要继续为预留资源付费,直到发生以下情况之一:
Compute Engine 会在您选择的日期和时间自动删除预留。
您删除了预留。
您减少了预留中的预留实例数量。
如果您有承诺使用折扣,则无论您是否使用这些资源,您都需要继续为所承诺的资源付费。
如果您保留了挂接到实例的任何资源(例如磁盘),则需要继续为这些资源付费,直到将其删除。
如需保留挂接到 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
如果您在一个或多个实例中启用了安全关停,则可以删除这些实例,而无需安全关停,或者手动结束正在进行的安全关停。为此,请在
gcloud beta compute instances delete
命令中使用--no-graceful-shutdown
标志。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 的凭据。
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
如需了解详情,请参阅 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
替换以下内容:
(可选)如果您已在一个或多个实例中启用平滑关停,则可以删除这些实例,而无需平滑关停它们,或者手动结束正在进行的安全关停。为此,请在
gcloud beta compute instances delete
命令中使用--no-graceful-shutdown
标志。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-06-24。
-