The following is an example of creating a dynamic group:
REST
To create a dynamic group, call
groups.create()
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:
The following is an example of updating a dynamic group:
REST
To update a dynamic group, call
groups.patch()
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:
# name (i.e. groups/01234abcdeef) uniquely identifies the groupdefupdate_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()returnresponse
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-25 UTC."],[[["\u003cp\u003eDynamic groups are available to specific Google Workspace and Cloud Identity premium accounts, with a limit of 500 per customer, which can be increased by contacting Google Workspace Support.\u003c/p\u003e\n"],["\u003cp\u003eCreating a dynamic group involves calling the \u003ccode\u003egroups.create()\u003c/code\u003e method (REST) or \u003ccode\u003eservice.groups().create\u003c/code\u003e (Python), ensuring the \u003ccode\u003edynamicGroupMetadata\u003c/code\u003e contains the query to populate the group.\u003c/p\u003e\n"],["\u003cp\u003eUpdating a dynamic group requires using \u003ccode\u003egroups.patch()\u003c/code\u003e (REST) or \u003ccode\u003eservice.groups().patch\u003c/code\u003e (Python), and includes modifying the \u003ccode\u003edynamicGroupMetadata\u003c/code\u003e with a new query.\u003c/p\u003e\n"],["\u003cp\u003eBefore creating or updating dynamic groups, users must set up Cloud Identity and familiarize themselves with the Groups API, dynamic groups overview, and set up the Groups API.\u003c/p\u003e\n"]]],[],null,["# Creating and updating a dynamic group\n=====================================\n\nThis page explains how to create and update a dynamic group.\n| **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\n\nBefore you begin\n----------------\n\n| **Note:** Before you use any of the Cloud Identity APIs, you must set up Cloud Identity. See [Setting up Cloud Identity](/identity/docs/set-up-cloud-identity-admin) for instructions.\n\nPerform the following tasks before proceeding with the information on this page:\n\n- Read the [Groups API overview](/identity/docs/groups).\n\n- Read the [dynamic groups overview](/identity/docs/concepts/overview-dynamic-groups).\n\n- [Set up the Groups API](/identity/docs/how-to/setup).\n\n- (Optional) [Formulate and test a membership query](/identity/docs/how-to/test-query-dynamic-groups).\n\nCreating a dynamic group\n------------------------\n\nThe following is an example of creating a dynamic group: \n\n### REST\n\n\nTo create a dynamic group, call\n[`groups.create()`](/identity/docs/reference/rest/v1/groups/create)\nwith an instance of the group. The instance of the group must contain the\n`dynamicGroupMetadata` with the query used to populate the group.\n\n### Python\n\nTo create a dynamic group, call the `service.groups().create` method with\nan instance of the group. The instance of the group must contain the\n`dynamicGroupMetadata` with the query used to populate the group. The\nfollowing sample shows how to create a dynamic group containing all users\nexisting in the Engineering department: \n\n def create_dynamic_group(customer_id, email, query):\n service = build_service()\n groupDef = {\n \"parent\": \"customerId/{}\".format(customer_id),\n \"groupKey\": {\"id\": email},\n \"labels\": {\"cloudidentity.googleapis.com/groups.discussion_forum\": \"\"},\n \"dynamicGroupMetadata\": {\n \"queries\": [\n {\n \"resourceType\": \"USER\",\n \"query\": \"user.organizations.exists(org, org.department=='engineering')\"\n }\n ]\n }\n }\n request = service.groups().create(body=groupDef)\n request.uri += \"&initialGroupConfig=EMPTY\"\n response = request.execute()\n return response\n\nUpdating a dynamic group\n------------------------\n\nThe following is an example of updating a dynamic group: \n\n### REST\n\n\nTo update a dynamic group, call\n[`groups.patch()`](/identity/docs/reference/rest/v1/groups/patch)\nwith an instance of the group. The instance of the group must contain the\n`dynamicGroupMetadata` with the new query used to populate the group.\n\n### Python\n\nTo update a dynamic group, call the `service.groups().patch` method with\nthe name of the group to update and an instance of the group. The instance\nof the group must contain the\n`dynamicGroupMetadata` with the query used to populate the group. The\nfollowing sample shows how to update a dynamic group containing all users in\nEngineering to a group containing all users in either Engineering or Finance\ndepartments: \n\n # name (i.e. groups/01234abcdeef) uniquely identifies the group\n def update_dynamic_group(name, query):\n service = build_service()\n groupDef = {\n \"dynamicGroupMetadata\": {\n \"queries\": [\n {\n \"resourceType\": \"USER\",\n \"query\": \"user.organizations.exists(org, org.department=='engineering' || org.department=='finance')\"\n }\n ]\n }\n }\n request = service.groups().patch(name=name, body=groupDef)\n request.uri += \"&updateMask=dynamicGroupMetadata\"\n response = request.execute()\n\n return response\n\nWhat's next\n-----------\n\nAfter the dynamic group exists, you can retrieve it and list its memberships.\nFor more information, refer to\n[Retrieving a dynamic group and listing members](/identity/docs/how-to/retrieve-list-dynamic-groups)."]]