# Creating and updating a dynamic group

This page explains how to create and update a dynamic group.

> [!NOTE]
> **Note:** Dynamic groups are only available to Google Workspace Enterprise Standard, Enterprise Plus, Enterprise for Education, and Cloud Identity premium accounts. You can create up to 500 dynamic groups per customer. This limit can be increased on a case-by-case basis---contact [Google Workspace Support](https://support.google.com/a/answer/1047213) with your specific use case to request an increase.wss

## Before you begin

> [!NOTE]
> **Note:** Before you use any of the Cloud Identity APIs, you must set up Cloud Identity. See [Setting up Cloud Identity](https://docs.cloud.google.com/identity/docs/set-up-cloud-identity-admin) for instructions.

Perform the following tasks before proceeding with the information on this page:

- Read the [Groups API overview](https://docs.cloud.google.com/identity/docs/groups.md.txt).

- Read the [dynamic groups overview](https://docs.cloud.google.com/identity/docs/concepts/overview-dynamic-groups.md.txt).

- [Set up the Groups API](https://docs.cloud.google.com/identity/docs/how-to/setup.md.txt).

- (Optional) [Formulate and test a membership query](https://docs.cloud.google.com/identity/docs/how-to/test-query-dynamic-groups.md.txt).

## Creating a dynamic group

The following is an example of creating a dynamic group:

### REST


To create a dynamic group, call
[`groups.create()`](https://docs.cloud.google.com/identity/docs/reference/rest/v1/groups/create.md.txt)
with an instance of the group. The instance of the group must contain the
`dynamicGroupMetadata` with the query used to populate the group.

### Python

To create a dynamic group, call the `service.groups().create` method with
an instance of the group. The instance of the group must contain the
`dynamicGroupMetadata` with the query used to populate the group. The
following sample shows how to create a dynamic group containing all users
existing in the Engineering department:

```python
def create_dynamic_group(customer_id, email, query):
  service = build_service()
  groupDef = {
      "parent": "customerId/{}".format(customer_id),
      "groupKey": {"id": email},
      "labels": {"cloudidentity.googleapis.com/groups.discussion_forum": ""},
      "dynamicGroupMetadata": {
          "queries": [
              {
                  "resourceType": "USER",
                  "query": "user.organizations.exists(org, org.department=='engineering')"
              }
          ]
      }
  }
  request = service.groups().create(body=groupDef)
  request.uri += "&initialGroupConfig=EMPTY"
  response = request.execute()
  return response
```

## Updating a dynamic group

The following is an example of updating a dynamic group:

### REST


To update a dynamic group, call
[`groups.patch()`](https://docs.cloud.google.com/identity/docs/reference/rest/v1/groups/patch.md.txt)
with an instance of the group. The instance of the group must contain the
`dynamicGroupMetadata` with the new query used to populate the group.

### Python

To update a dynamic group, call the `service.groups().patch` method with
the name of the group to update and an instance of the group. The instance
of the group must contain the
`dynamicGroupMetadata` with the query used to populate the group. The
following sample shows how to update a dynamic group containing all users in
Engineering to a group containing all users in either Engineering or Finance
departments:

```python
# name (i.e. groups/01234abcdeef) uniquely identifies the group
def update_dynamic_group(name, query):
  service = build_service()
  groupDef = {
    "dynamicGroupMetadata": {
    "queries": [
        {
          "resourceType": "USER",
          "query": "user.organizations.exists(org, org.department=='engineering' || org.department=='finance')"
        }
      ]
    }
  }
  request = service.groups().patch(name=name, body=groupDef)
  request.uri += "&updateMask=dynamicGroupMetadata"
  response = request.execute()

  return response
```

## What's next

After the dynamic group exists, you can retrieve it and list its memberships.
For more information, refer to
[Retrieving a dynamic group and listing members](https://docs.cloud.google.com/identity/docs/how-to/retrieve-list-dynamic-groups.md.txt).