An abstraction representing a collected group of updates / deletes.
Used to build up a bulk mutation.
For example, the following snippet of code will put the two save
operations and the delete operation into the same mutation, and send
them to the server in a single API request:
[[["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-09-04 UTC."],[],[],null,["# Module batch (2.21.0)\n\nVersion latestkeyboard_arrow_down\n\n- [2.21.0 (latest)](/python/docs/reference/datastore/latest/google.cloud.datastore.batch)\n- [2.20.2](/python/docs/reference/datastore/2.20.2/google.cloud.datastore.batch)\n- [2.19.0](/python/docs/reference/datastore/2.19.0/google.cloud.datastore.batch)\n- [2.18.0](/python/docs/reference/datastore/2.18.0/google.cloud.datastore.batch)\n- [2.17.0](/python/docs/reference/datastore/2.17.0/google.cloud.datastore.batch)\n- [2.16.1](/python/docs/reference/datastore/2.16.1/google.cloud.datastore.batch)\n- [2.15.2](/python/docs/reference/datastore/2.15.2/google.cloud.datastore.batch)\n- [2.14.0](/python/docs/reference/datastore/2.14.0/google.cloud.datastore.batch)\n- [2.13.2](/python/docs/reference/datastore/2.13.2/google.cloud.datastore.batch)\n- [2.12.0](/python/docs/reference/datastore/2.12.0/google.cloud.datastore.batch)\n- [2.11.1](/python/docs/reference/datastore/2.11.1/google.cloud.datastore.batch)\n- [2.10.0](/python/docs/reference/datastore/2.10.0/google.cloud.datastore.batch)\n- [2.9.0](/python/docs/reference/datastore/2.9.0/google.cloud.datastore.batch)\n- [2.8.3](/python/docs/reference/datastore/2.8.3/google.cloud.datastore.batch)\n- [2.7.2](/python/docs/reference/datastore/2.7.2/google.cloud.datastore.batch)\n- [2.6.2](/python/docs/reference/datastore/2.6.2/google.cloud.datastore.batch)\n- [2.5.1](/python/docs/reference/datastore/2.5.1/google.cloud.datastore.batch)\n- [2.4.0](/python/docs/reference/datastore/2.4.0/google.cloud.datastore.batch)\n- [2.3.0](/python/docs/reference/datastore/2.3.0/google.cloud.datastore.batch)\n- [2.2.0](/python/docs/reference/datastore/2.2.0/google.cloud.datastore.batch)\n- [2.1.6](/python/docs/reference/datastore/2.1.6/google.cloud.datastore.batch)\n- [2.0.1](/python/docs/reference/datastore/2.0.1/google.cloud.datastore.batch)\n- [1.15.5](/python/docs/reference/datastore/1.15.5/google.cloud.datastore.batch)\n- [1.14.0](/python/docs/reference/datastore/1.14.0/google.cloud.datastore.batch)\n- [1.13.2](/python/docs/reference/datastore/1.13.2/google.cloud.datastore.batch)\n- [1.12.0](/python/docs/reference/datastore/1.12.0/google.cloud.datastore.batch)\n- [1.11.0](/python/docs/reference/datastore/1.11.0/google.cloud.datastore.batch)\n- [1.10.0](/python/docs/reference/datastore/1.10.0/google.cloud.datastore.batch)\n- [1.9.0](/python/docs/reference/datastore/1.9.0/google.cloud.datastore.batch) \nCreate / interact with a batch of updates / deletes.\n\nBatches provide the ability to execute multiple operations\nin a single request to the Cloud Datastore API.\n\nSee\n\u003chttps://cloud.google.com/datastore/docs/concepts/entities#batch_operations\u003e\n\nClasses\n-------\n\n### [Batch](/python/docs/reference/datastore/latest/google.cloud.datastore.batch.Batch)\n\n Batch(client)\n\nAn abstraction representing a collected group of updates / deletes.\n\nUsed to build up a bulk mutation.\n\nFor example, the following snippet of code will put the two `save`\noperations and the `delete` operation into the same mutation, and send\nthem to the server in a single API request:\n\n.. testsetup:: batch \n\n import uuid\n\n from google.cloud import https://cloud.google.com/python/docs/reference/datastore/latest/\n\n unique = str(uuid.uuid4())[0:8]\n client = https://cloud.google.com/python/docs/reference/datastore/latest/.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html(namespace='ns{}'.format(unique))\n\n.. doctest:: batch \n\n \u003e\u003e\u003e entity1 = datastore.Entity(client.key('EntityKind', 1234))\n \u003e\u003e\u003e entity2 = datastore.Entity(client.key('EntityKind', 2345))\n \u003e\u003e\u003e key3 = client.key('EntityKind', 3456)\n \u003e\u003e\u003e batch = client.batch()\n \u003e\u003e\u003e batch.begin()\n \u003e\u003e\u003e batch.put(entity1)\n \u003e\u003e\u003e batch.put(entity2)\n \u003e\u003e\u003e batch.delete(key3)\n \u003e\u003e\u003e batch.commit()\n\nYou can also use a batch as a context manager, in which case\n`commit` will be called automatically if its block exits without\nraising an exception:\n\n.. doctest:: batch \n\n \u003e\u003e\u003e with client.batch() as batch:\n ... batch.put(entity1)\n ... batch.put(entity2)\n ... batch.delete(key3)\n\nBy default, no updates will be sent if the block exits with an error:\n\n.. doctest:: batch \n\n \u003e\u003e\u003e def do_some_work(batch):\n ... return\n \u003e\u003e\u003e with client.batch() as batch:\n ... do_some_work(batch)\n ... raise Exception() # rolls back\n Traceback (most recent call last):\n ...\n Exception\n\n.. testcleanup:: txn \n\n with client.batch() as batch:\n batch.delete(client.key('EntityKind', 1234))\n batch.delete(client.key('EntityKind', 2345))"]]