このドキュメントでは、インスタンス リソースで deletionProtection
プロパティを設定することによって特定の VM インスタンスが削除されないようにする方法について説明します。VM インスタンスの詳細については、インスタンスのドキュメントをご覧ください。
ワークロードの一部として、アプリケーションやサービスの実行に不可欠な VM インスタンスが存在する場合があります。たとえば、SQL サーバーを実行するインスタンスや、ライセンス マネージャーとして使用されるサーバーなどがこれに該当します。このような VM インスタンスは常に稼働していなければならないため、これらの VM が削除されないようにする手段が必要です。
deletionProtection
フラグを設定することで、VM インスタンスが誤って削除される事態を防ぐことができます。deletionProtection
フラグが設定された VM インスタンスをユーザーが削除しようとすると、そのリクエストは失敗します。このフラグをリセットしてリソースを削除可能にできるのは、compute.instances.create
権限を持つロールを付与されたユーザーだけです。
準備
- インスタンスのドキュメントを読みます。
-
まだ設定していない場合は、認証を設定します。認証とは、 Google Cloud サービスと API にアクセスするために ID を確認するプロセスです。ローカル開発環境からコードまたはサンプルを実行するには、次のいずれかのオプションを選択して 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 内からのインスタンスの終了(
shutdown
コマンドを実行するなど) - インスタンスの停止
- インスタンスのリセット
- インスタンスの一時停止
- 検出された不正行為や悪用を理由とした Google によるインスタンスの削除
- プロジェクトの終了を理由としたインスタンスの削除
- VM 内からのインスタンスの終了(
削除保護は、通常の VM とプリエンプティブル VM の両方に適用できます。
削除保護は、マネージド インスタンス グループに属する VM には適用できませんが、非マネージド インスタンス グループに属するインスタンスには適用できます。
削除保護は、インスタンス テンプレートでは指定できません。
compute.instances.create
compute.admin
compute.instanceAdmin.v1
Google Cloud コンソールで [インスタンスの作成] ページに移動します。
[詳細オプション] セクションを開き、次の操作を行います。
- [管理] セクションを開きます。
- [削除からの保護の有効化] チェックボックスをオンにします。
VM 作成プロセスを続行します。
[VM インスタンス] ページに移動します。
プロンプトが表示されたら、プロジェクトを選択して [続行] をクリックします。
[VM インスタンス] ページの [列を選択] プルダウン メニューで [削除からの保護] を有効にします。
新しい列が追加され、削除保護アイコンが表示されます。VM で削除保護が有効になっている場合、インスタンス名の横にアイコンが表示されます。
[VM インスタンス] ページに移動します。
プロンプトが表示されたら、プロジェクトを選択して [続行] をクリックします。
削除保護を切り替えるインスタンスの名前をクリックします。インスタンスの詳細ページが表示されます。
インスタンスの詳細ページで、次の手順を実行します。
- ページの上部にある [編集] ボタンをクリックします。
[削除からの保護] で、チェックボックスをオンにして削除保護を有効にするか、チェックボックスをオフにして削除保護を無効にします。
変更を保存します。
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 認証ドキュメントの 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 で確認できます。Console
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]
既存インスタンスの削除保護を切り替える
インスタンスの現在のステータスにかかわらず、既存インスタンスの削除保護を切り替えることができます。削除保護を有効または無効にする前にインスタンスを停止する必要はありません。
Console
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
に設定します。リクエスト本文は提供しないでください。次のステップ
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-11 UTC。
-