本文件說明如何在執行個體資源上設定 deletionProtection
屬性,以免刪除特定的 VM 執行個體。如要進一步瞭解 VM 執行個體,請參閱執行個體說明文件。
在執行工作負載時,某些 VM 執行個體對應用程式或服務的執行可能尤其重要,例如執行 SQL 伺服器的執行個體、用作授權管理員的伺服器等。這些 VM 執行個體可能需要時時刻刻保持運作,因此得採取保護措施,以免 VM 遭到刪除。
設定 deletionProtection
旗標,可保護 VM 執行個體以防不慎刪除。如有使用者嘗試刪除設有 deletionProtection
旗標的 VM 執行個體,該要求會失敗。使用者的角色必須具有 compute.instances.create
權限,才可重設旗標,允許刪除資源。
事前準備
- 參閱執行個體說明文件。
-
如果尚未設定驗證,請先完成設定。
「驗證」是指驗證身分的程序,確認您有權存取 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.
防刪除功能無法防止下列動作:
一般 VM 和先佔 VM 均可使用防刪除功能。
防刪除功能無法使用在屬於代管執行個體群組的 VM 上,但可用在屬於非受管執行個體群組的執行個體上。
無法在執行個體範本中指定防刪除功能。
compute.instances.create
compute.admin
compute.instanceAdmin.v1
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
展開「Advanced options」(進階選項) 區段,然後執行下列操作:
- 展開「管理」部分。
- 勾選「Enable deletion protection」(啟用防刪除功能) 核取方塊。
繼續執行 VM 建立程序。
前往「VM instances」(VM 執行個體) 頁面。
如果出現系統提示,請選取您的專案,然後按一下 [Continue] (繼續)。
在「VM instances」(VM 執行個體) 頁面上,下拉 [Columns] (欄) 選單,然後啟用 [Deletion protection] (防刪除功能)。
此時會顯示具有防刪除功能圖示的新欄。如果 VM 已啟用防刪除功能,執行個體名稱旁會出現圖示。
前往「VM instances」(VM 執行個體) 頁面。
如果出現系統提示,請選取您的專案,然後按一下 [Continue] (繼續)。
按一下要切換防刪除功能的執行個體名稱。執行個體詳細資料頁面隨即會顯示。
請在執行個體詳細資料頁面上,完成以下步驟:
- 按一下頁面頂端的 [Edit] (編輯) 按鈕。
在「Deletion Protection」(防刪除功能) 下方,勾選方塊可啟用防刪除功能,取消勾選則可停用。
儲存變更。
Go
如要在本機開發環境中使用本頁的 Go 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
Java
如要在本機開發環境中使用本頁的 Java 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
Node.js
如要在本機開發環境中使用本頁的 Node.js 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
PHP
如要在本機開發環境中使用本頁的 PHP 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
Python
如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
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 驗證說明文件中的「Authenticate for using REST」。
規格
權限
如要執行這項工作,您必須擁有以下權限或者對資源具有以下 IAM 角色。
權限
角色
於建立執行個體時設定防刪除功能
根據預設,系統會停用執行個體的刪除保護功能。如要啟用防刪除功能,請參閱以下操作說明。
主控台
gcloud
建立 VM 執行個體時,請加入
--deletion-protection
或no-deletion-protection
旗標。根據預設,系統會停用防刪除功能。如要啟用,請使用:gcloud compute instances create [INSTANCE_NAME] --deletion-protection
其中的
[INSTANCE_NAME]
是需要的執行個體的名稱。於建立時停用防刪除功能:
gcloud compute instances create [INSTANCE_NAME] --no-deletion-protection
Go
Java
Node.js
Python
REST
在 API 中建立 VM 執行個體時,請在要求主體中加入
deletionProtection
屬性。例如:POST https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/instances { "name": "[INSTANCE_NAME]", "deletionProtection": "true", ... }
如要停用防刪除功能,請將
deletionProtection
設為false
。判斷執行個體是否已啟用防刪除功能
您可以判斷執行個體是否已在
gcloud tool
或 API 中啟用防刪除功能。主控台
gcloud
在 gcloud CLI 中,執行
instances describe
指令,並搜尋防刪除功能的欄位。例如:gcloud compute instances describe example-instance | grep "deletionProtection"
該工具會傳回
deletionProtection
屬性的值,並設為true
或false
:deletionProtection: false
Go
Java
Node.js
Python
REST
在 API 中,發出
GET
要求並尋找deletionProtection
欄位:GET https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/instances/[INSTANCE_NAME]
切換現有執行個體的防刪除功能
無論執行個體的目前狀態為何,均可切換現有執行個體的防刪除功能。確切來說,不用停止執行個體,也可以啟用或停用防刪除功能。
主控台
gcloud
使用 gcloud CLI,以
--deletion-protection
或--no-deletion-protection
旗標執行update
指令:gcloud compute instances update [INSTANCE_NAME] \ [--deletion-protection | --no-deletion-protection]
舉例來說,如要啟用名為
example-vm
的執行個體的防刪除功能:gcloud compute instances update example-vm --deletion-protection
Go
Java
Node.js
Python
REST
在 API 中,可使用
deletionProtection
查詢參數,向setDeletionProtection
方法發出POST
要求。例如:POST https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/instances/[INSTANCE_NAME]/setDeletionProtection?deletionProtection=true
如要停用防刪除功能,請將
deletionProtection
設為false
。請勿透過此要求提供要求主體。後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-09 (世界標準時間)。
-