删除和恢复删除的服务账号

本页面介绍了如何使用 Identity and Access Management (IAM) API、 Google Cloud 控制台和 gcloud 命令行工具删除和恢复删除服务账号。

准备工作

  • Enable the IAM API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  • 设置身份验证。

    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

    In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

    C#

    如需在本地开发环境中使用本页面上的 .NET 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      安装 Google Cloud CLI。

      如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

      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.

    Google Cloud

    C++

    如需在本地开发环境中使用本页面上的 C++ 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      安装 Google Cloud CLI。

      如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

      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.

    如需了解详情,请参阅 Google Cloud 身份验证文档中的为本地开发环境设置 ADC

    Go

    如需在本地开发环境中使用本页面上的 Go 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      安装 Google Cloud CLI。

      如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

      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.

    Google Cloud

    Java

    如需在本地开发环境中使用本页面上的 Java 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      安装 Google Cloud CLI。

      如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

      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.

    Google Cloud

    Python

    如需在本地开发环境中使用本页面上的 Python 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      安装 Google Cloud CLI。

      如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

      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.

    Google Cloud

    REST

    如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭证。

      安装 Google Cloud CLI。

      如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

    如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证

  • 了解 IAM 服务账号

所需的角色

如需获得删除和恢复删除的服务账号所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义角色或其他预定义角色来获取所需的权限。

如需详细了解这些角色,请参阅服务账号角色

IAM 基本角色也具有管理服务账号的权限。 您不应在生产环境中授予基本角色,但可以在开发或测试环境中授予这些角色。

删除服务账号

您删除服务账号后,应用将无法再通过该服务账号访问Google Cloud 资源。如果您删除默认的 App Engine 和 Compute Engine 服务账号,则使用这些服务账号的 App Engine 应用和 Compute Engine 虚拟机实例将无法再访问项目中的资源。

请谨慎删除服务账号。在删除之前,请确保您的关键应用不再使用该服务账号。如果您不确定某个服务账号是否正在使用,Google 建议您停用该服务账号,而不是将其删除。已停用的服务账号可以在需要时重新启用。 如需了解详情,请参阅停用未使用的服务账号,然后再将其删除

如果您要恢复已删除的服务账号,可以恢复删除(如果自删除服务账号后已过去 30 天或更短时间)。30 天后,IAM 会永久移除该服务账号。服务账号被永久移除后,即使您提交支持请求, Google Cloud也无法将其恢复。

如需进一步降低删除必要服务账号的风险,您还可以启用更改风险建议。如果您尝试删除 Google Cloud 已确定为重要账号的服务账号,更改风险建议会生成警告。

如果您删除服务账号,然后创建一个具有相同名称的新服务账号,新服务账号将被视为单独的身份;它不会继承那些授予已删除的服务账号的角色。相反,如果您删除某个服务账号,然后恢复删除,那么该服务账号的身份不会更改,并且服务账号会保留其角色。

删除服务账号时,其角色绑定不会立即移除;它们将在 60 天内自动从系统中清除。在此之前,该服务账号显示在角色绑定中,并且带有 deleted: 前缀和 ?uid=NUMERIC_ID 后缀,其中 NUMERIC_ID 是服务账号的唯一数字 ID。

已删除的服务账号不计入您的服务账号配额

控制台

  1. 在 Google Cloud 控制台中,打开服务账号页面。

    转到“服务账号”

  2. 选择一个项目。

  3. 选择要删除的服务账号,然后点击删除

gcloud

  1. In the Google Cloud console, activate Cloud Shell.