# Manage consumer policies

> [!WARNING]
>
> **Preview
> --- Hierarchical service activation**
>
>
> This feature is
>
> subject to the "Pre-GA Offerings Terms" in the General Service Terms section of the
> [Service Specific
> Terms](https://docs.cloud.google.com/terms/service-terms#1).
>
> Pre-GA features are available "as is" and might have limited support.
>
> For more information, see the
> [launch stage descriptions](https://cloud.google.com/products/#product-launch-stages).

[Hierarchical service activation](https://docs.cloud.google.com/service-usage/docs/hierarchical-service-activation/overview.md.txt)
lets you manage service enablement that is inherited through the Google Cloud
resource hierarchy.

At each level of the Google Cloud resource hierarchy, a Service Usage
`ConsumerPolicy` resource named `default` contains an `enableRules` parameter
that defines which services are enabled for that resource. Rather than managing
individual services directly, you modify the `ConsumerPolicy` resource to
control service enablement. This guide demonstrates using the Google Cloud CLI to
manage consumer policies across projects, folders, and organizations.

## Before you begin

1. If you don't indicate the Google Cloud resource that you want to manage, the commands in this document default to the current project. For example, `gcloud
   beta services policies get --policy-name=default` returns the consumer policy for the current project.

   You can retrieve the current project ID:

   ```bash
   gcloud config list --format='text(core.project)'
   ```

<br />

### Required roles


To get the permissions that
you need to manage consumer policies,

ask your administrator to grant you the
[Service Usage Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/serviceusage.md.txt#serviceusage.serviceUsageAdmin) (`roles/serviceusage.serviceUsageAdmin`) IAM role on your target resource.


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


This predefined role contains

the permissions required to manage consumer policies. To see the exact permissions that are
required, expand the **Required permissions** section:


#### Required permissions

The following permissions are required to manage consumer policies:

- Get a consumer policy: `serviceusage.consumerpolicy.get`
- Update a consumer policy:
  - `serviceusage.consumerpolicy.analyze`
  - `serviceusage.consumerpolicy.update`
- Get an effective policy: `serviceusage.effectivepolicy.get`
- Check if a service is enabled: `serviceusage.values.test`


You might also be able to get
these permissions
with [custom roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles.md.txt) or
other [predefined roles](https://docs.cloud.google.com/iam/docs/roles-overview.md.txt#predefined).

## Get a consumer policy

You can retrieve the `ConsumerPolicy` for a project, folder, or organization by
using the
[`gcloud beta services policies get`](https://docs.cloud.google.com/sdk/gcloud/reference/beta/services/policies/get.md.txt)
command.

This command returns the `enableRules` set at that exact resource level, and not
inherited rules. For a more complete view that includes inherited rules, you can
[retrieve the effective policy](https://docs.cloud.google.com/service-usage/docs/hierarchical-service-activation/manage-consumer-policies.md.txt#get-effective). Only the `default` policy name
is supported.

```bash
gcloud beta services policies get --policy-name=default \
    --RESOURCE_TYPE=RESOURCE_ID \
    --output-file=OUTPUT_FILE
```

Replace the following:

- `RESOURCE_TYPE`: the type of resource that you want
  to get the consumer policy for. Valid values are `project`, `folder`, and
  `organization`. You can omit this flag to return the consumer policy for the
  current project.

- `RESOURCE_ID`: the
  [project ID](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects.md.txt),
  [folder ID](https://docs.cloud.google.com/resource-manager/docs/creating-managing-folders.md.txt), or
  [organization ID](https://docs.cloud.google.com/resource-manager/docs/creating-managing-organization.md.txt#retrieving_your_organization_id),
  depending on the value you used for `RESOURCE_TYPE`.

- `OUTPUT_FILE`: (optional) the path to an output file
  where the contents of the consumer policy can be saved. Supported file
  formats are JSON and YAML.

## Update a consumer policy

You can update a `ConsumerPolicy` by using the
[`gcloud beta services policies update`](https://docs.cloud.google.com/sdk/gcloud/reference/beta/services/policies/update.md.txt)
command. This lets you replace the current policy and use a YAML file to update
many enabled services at once.

This command doesn't automatically enable
[service dependencies](https://docs.cloud.google.com/service-usage/docs/hierarchical-service-activation/overview.md.txt#service_dependencies).
You must list all service dependencies explicitly in the input file. You can use
the `--validate-only` flag to preview changes before applying them.

```bash
gcloud beta services policies update --consumer-policy-file=PATH_TO_CONSUMER_POLICY
```

Replace `PATH_TO_CONSUMER_POLICY` with the path to the
YAML file that contains the consumer policy---for example:

`gcloud beta services policies update --consumer-policy-file=/path/to/the/file.yaml`

Supported flags:

- `--bypass-api-usage-check` to bypass usage checks. Otherwise, the system
  returns an error if any service you want to disable was used in the last
  30 days or enabled in the last 3 days.

- `--bypass-dependency-check` to bypass the check for dependencies. When
  adding a service, all service dependencies must be present in the policy.
  When removing a service, no other services in the policy can depend on it.
  Otherwise, the update doesn't succeed and an error is returned.

- `--validate-only` to validate the update without actually performing
  the operation.

Note the following:

- To get a YAML template for the policy file, first
  [retrieve the consumer policy](https://docs.cloud.google.com/service-usage/docs/hierarchical-service-activation/manage-consumer-policies.md.txt#get) and use the `--output-file` flag to save
  the policy to a file. Then, modify this file and use it as input for the update
  command. Sample template:

  ```yaml
  name: projects/PROJECT_ID/consumerPolicies/default
  enableRules:
    - services:
      - services/SERVICE
      - ...
      - ...
  ```

  Replace the following:
  - `PROJECT_ID`: your
    [Google Cloud project ID](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects.md.txt).
    Other supported resource names follow a similar format---for example:
    `folders/FOLDER_ID/consumerPolicies/default`.

  - `SERVICE`: the name of the service---for example:
    `bigquery.googleapis.com`.

## Get an effective policy

You can retrieve the effective policy for a resource by using the
[`gcloud beta services policies get-effective`](https://docs.cloud.google.com/sdk/gcloud/reference/beta/services/policies/get-effective)
command.

The effective policy combines the resource's `ConsumerPolicy` with the policies
of all its ancestors. This gives you a complete view of all enabled services,
including all services enabled directly on the resource, and those inherited
from its ancestors.

```bash
gcloud beta services policies get-effective \
    --RESOURCE_TYPE=RESOURCE_ID \
    --view=VIEW
```

Replace the following:

- `RESOURCE_TYPE`: the type of resource that you want to
  get the effective policy for. Valid values are `project`, `folder`,
  and `organization`. You can omit this flag to return the effective policy
  for the current project.

- `RESOURCE_ID`: the
  [project ID](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects.md.txt),
  [folder ID](https://docs.cloud.google.com/resource-manager/docs/creating-managing-folders.md.txt), or
  [organization ID](https://docs.cloud.google.com/resource-manager/docs/creating-managing-organization.md.txt#retrieving_your_organization_id),
  depending on the value you used for `RESOURCE_TYPE`.

- `VIEW`: either `BASIC` to retrieve a limited set of
  metadata or `FULL` to retrieve the full set of metadata, including where
  each service was enabled in the hierarchy. Default is `BASIC`.

## Check if a service is enabled

You can check if a service is enabled on a resource by using the
[`gcloud beta services policies test-enabled`](https://docs.cloud.google.com/sdk/gcloud/reference/beta/services/policies/test-enabled)
command.

When checking the status of a service, this command considers the effective
policy and the full Google Cloud resource hierarchy.

```bash
gcloud beta services policies test-enabled SERVICE \
    --RESOURCE_TYPE=RESOURCE_ID
```

Replace the following:

- `SERVICE`: the name of the service you want to check.

- `RESOURCE_TYPE`: the type of resource that you want
  to check. Valid values are `project`, `folder`, and `organization`.
  You can omit this flag to check the status for the current project.

- `RESOURCE_ID`: the
  [project ID](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects.md.txt),
  [folder ID](https://docs.cloud.google.com/resource-manager/docs/creating-managing-folders.md.txt), or
  [organization ID](https://docs.cloud.google.com/resource-manager/docs/creating-managing-organization.md.txt#retrieving_your_organization_id),
  depending on the value you used for `RESOURCE_TYPE`.

## Examples

#### Retrieving consumer policy

- Retrieving consumer policy for current project:

  ```bash
  gcloud beta services policies get
  ```

  Output:

  ```yaml
  name: projects/PROJECT_ID/consumerPolicies/default
  enableRules:
   services:
    - services/apikeys.googleapis.com
    - services/compute.googleapis.com
    - services/oslogin.googleapis.com
    - services/serviceusage.googleapis.com
  updateTime: 2025-09-11T23:05:22.758394Z
  createTime: 2025-01-31T20:17:37.272343Z
  etag: W/"W9nsVJK0V1m7ee7tM7pFDg=="
  ```

#### Updating consumer policy

- Successfully updating consumer policy:

  ```bash
  gcloud beta services policies update --consumer-policy-file=/tmp/test.yaml
  ```

  Output:

  ```yaml
  Operation [operations/ucpat.p39-581601899707-86fe7e37-c7f9-4624-90c4-d54754d57eed] complete. Result: {
      "@type":"type.googleapis.com/google.api.serviceusage.v2beta.ConsumerPolicy",
      "createTime":"1970-01-01T00:00:00Z",
      "enableRules":[
          {
              "services":[
                  "services/apikeys.googleapis.com"
              ]
          }
      ],
      "name":"projects/PROJECT_ID/consumerPolicies/default",
      "updateTime":"1970-01-01T00:00:00Z"
  }
  ```
- Unsuccessfully updating consumer policy:

  ```bash
  gcloud beta services policies update --consumer-policy-file=/tmp/test.json
  ```

  Error message:

  ```yaml
  ERROR: (gcloud.beta.services.policies.update) Invalid consumer_policy_file format. Please provide path to a yaml file.
  ```
- Attempting to update consumer policy when service has been used or enabled recently:

  ```bash
  gcloud beta services policies update --consumer-policy-file=/tmp/test.yaml
  ```

  Output:

  ```yaml
  The operation "operations/ucpat.p39-581601899707-4884bdb0-d899-49ac-9c83-38457950ef33" resulted in a failure "The services apikeys.googleapis.com have usage in the last 30 days or were enabled in the past 3 days. Please specify force if you want to proceed with the destructive policy change. Help Token: AVSZLmtCfGwMm4oHmOyExdDEFRQ0pFiBrl879nbZlY2JkfVzeU63u66ApXYr6MIEAoig..."
  ```
- Updating consumer policy when service has been used or enabled recently by bypassing usage check:

  ```bash
  gcloud beta services policies update --consumer-policy-file=/tmp/test.yaml --bypass-api-usage-check
  ```

  Output:

  ```yaml
  Operation [operations/ucpat.p39-581601899707-8ffda670-ed04-4776-8d43-d2f8e1817e00] complete. Result: {
      "@type":"type.googleapis.com/google.api.serviceusage.v2beta.ConsumerPolicy",
      "createTime":"1970-01-01T00:00:00Z",
      "enableRules":[
          {
              "services":[
                  "services/serviceusage.googleapis.com"
              ]
          }
      ],
      "name":"projects/PROJECT_ID/consumerPolicies/default",
      "updateTime":"1970-01-01T00:00:00Z"
  }
  ```

#### Retrieving effective policy

- Retrieving `BASIC` view of the effective policy for current project:

  ```bash
  gcloud beta services policies get-effective
  ```

  Output:

  ```yaml
  EnabledRules:
   Services:
    -   services/apikeys.googleapis.com
    -   services/compute.googleapis.com
    -   services/computescanning.googleapis.com
    -   services/containeranalysis.googleapis.com
    -   services/containerscanning.googleapis.com
    -   services/gkebackup.googleapis.com
    -   services/osconfig.googleapis.com
    -   services/oslogin.googleapis.com
    -   services/serviceusage.googleapis.com
  ```
- Retrieving `FULL` view of the effective policy for current project:

  ```bash
  gcloud beta services policies get-effective --view=FULL
  ```

  Output:

  ```yaml
  EnabledRules:
   Services:
    -   services/apikeys.googleapis.com
    -   services/compute.googleapis.com
    -   services/computescanning.googleapis.com
    -   services/containeranalysis.googleapis.com
    -   services/containerscanning.googleapis.com
    -   services/gkebackup.googleapis.com
    -   services/osconfig.googleapis.com
    -   services/oslogin.googleapis.com
    -   services/serviceusage.googleapis.com

  Metadata of effective policy:
  EnabledService: services/apikeys.googleapis.com
  EnabledPolicies: ['projects/PROJECT_ID/consumerPolicies/default']

  EnabledService: services/bcidcloudenforcer-pa.googleapis.com
  EnabledPolicies: ['folders/FOLDER_ID/consumerPolicies/default']

  EnabledService: services/compute.googleapis.com
  EnabledPolicies: ['projects/PROJECT_ID/consumerPolicies/default']
  ...
  ```

#### Checking if a service is enabled

- Checking the status of enabled API keys service for current project:

  ```bash
  gcloud beta services policies test-enabled apikeys.googleapis.com
  ```

  Output:

  ```yaml
  Service apikeys.googleapis.com is ENABLED for resource projects/test-project.
  ```
- Checking the status of disabled App Hub service for current project:

  ```bash
  gcloud beta services policies test-enabled apphub.googleapis.com
  ```

  Output:

  ```yaml
  Service apphub.googleapis.com is NOT ENABLED for resource projects/test-project.
  ```

## What's next

- To enable and disable services, see [Manage service enablement](https://docs.cloud.google.com/service-usage/docs/hierarchical-service-activation/manage-enablement.md.txt).
- To inspect service dependencies, see [List service dependencies](https://docs.cloud.google.com/service-usage/docs/hierarchical-service-activation/list-dependencies.md.txt).