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:
Every batch is committed with a single commit request containing all
the work to be done as mutations. Inside a batch, calling put
with an entity, or delete with a key, builds up the request by
adding a new mutation. This getter returns the protobuf that has been
built-up so far.
Returns
Type
Description
iterable
The list of .datastore_pb2.Mutation protobufs to be sent in the commit request.
namespace
Getter for namespace in which the batch will run.
Returns
Type
Description
str
The namespace in which the batch will run.
project
Getter for project in which the batch will run.
Returns
Type
Description
str
The project in which the batch will run.
Methods
begin
begin()
Begins a batch.
This method is called automatically when entering a with
statement, however it can be called explicitly if you don't want
to use a context manager.
Overridden by xref_Transaction.
Exceptions
Type
Description
`ValueError
if the batch has already begun.
commit
commit(retry=None,timeout=None)
Commits the batch.
This is called automatically upon exiting a with statement,
however it can be called explicitly if you don't want to use a
context manager.
Parameters
Name
Description
retry
google.api_core.retry.Retry
A retry object used to retry requests. If None is specified, requests will be retried using a default configuration.
timeout
float
Time, in seconds, to wait for the request to complete. Note that if retry is specified, the timeout applies to each individual attempt.
if the batch is not in progress, if key is not complete, or if the key's project does not match ours.
put
put(entity)
Remember an entity's state to be saved during commit.
When an entity has a partial key, calling commit sends it as
an insert mutation and the key is completed. On return,
the key for the entity passed in is updated to match the key ID
assigned by the server.
[[["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-28 UTC."],[],[],null,["# Class Batch (2.21.0)\n\nVersion latestkeyboard_arrow_down\n\n- [2.21.0 (latest)](/python/docs/reference/datastore/latest/google.cloud.datastore.batch.Batch)\n- [2.20.2](/python/docs/reference/datastore/2.20.2/google.cloud.datastore.batch.Batch)\n- [2.19.0](/python/docs/reference/datastore/2.19.0/google.cloud.datastore.batch.Batch)\n- [2.18.0](/python/docs/reference/datastore/2.18.0/google.cloud.datastore.batch.Batch)\n- [2.17.0](/python/docs/reference/datastore/2.17.0/google.cloud.datastore.batch.Batch)\n- [2.16.1](/python/docs/reference/datastore/2.16.1/google.cloud.datastore.batch.Batch)\n- [2.15.2](/python/docs/reference/datastore/2.15.2/google.cloud.datastore.batch.Batch)\n- [2.14.0](/python/docs/reference/datastore/2.14.0/google.cloud.datastore.batch.Batch)\n- [2.13.2](/python/docs/reference/datastore/2.13.2/google.cloud.datastore.batch.Batch)\n- [2.12.0](/python/docs/reference/datastore/2.12.0/google.cloud.datastore.batch.Batch)\n- [2.11.1](/python/docs/reference/datastore/2.11.1/google.cloud.datastore.batch.Batch)\n- [2.10.0](/python/docs/reference/datastore/2.10.0/google.cloud.datastore.batch.Batch)\n- [2.9.0](/python/docs/reference/datastore/2.9.0/google.cloud.datastore.batch.Batch)\n- [2.8.3](/python/docs/reference/datastore/2.8.3/google.cloud.datastore.batch.Batch)\n- [2.7.2](/python/docs/reference/datastore/2.7.2/google.cloud.datastore.batch.Batch)\n- [2.6.2](/python/docs/reference/datastore/2.6.2/google.cloud.datastore.batch.Batch)\n- [2.5.1](/python/docs/reference/datastore/2.5.1/google.cloud.datastore.batch.Batch)\n- [2.4.0](/python/docs/reference/datastore/2.4.0/google.cloud.datastore.batch.Batch)\n- [2.3.0](/python/docs/reference/datastore/2.3.0/google.cloud.datastore.batch.Batch)\n- [2.2.0](/python/docs/reference/datastore/2.2.0/google.cloud.datastore.batch.Batch)\n- [2.1.6](/python/docs/reference/datastore/2.1.6/google.cloud.datastore.batch.Batch)\n- [2.0.1](/python/docs/reference/datastore/2.0.1/google.cloud.datastore.batch.Batch)\n- [1.15.5](/python/docs/reference/datastore/1.15.5/google.cloud.datastore.batch.Batch)\n- [1.14.0](/python/docs/reference/datastore/1.14.0/google.cloud.datastore.batch.Batch)\n- [1.13.2](/python/docs/reference/datastore/1.13.2/google.cloud.datastore.batch.Batch)\n- [1.12.0](/python/docs/reference/datastore/1.12.0/google.cloud.datastore.batch.Batch)\n- [1.11.0](/python/docs/reference/datastore/1.11.0/google.cloud.datastore.batch.Batch)\n- [1.10.0](/python/docs/reference/datastore/1.10.0/google.cloud.datastore.batch.Batch)\n- [1.9.0](/python/docs/reference/datastore/1.9.0/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))\n\nProperties\n----------\n\n### database\n\nGetter for database in which the batch will run.\n\n### mutations\n\nGetter for the changes accumulated by this batch.\n\nEvery batch is committed with a single commit request containing all\nthe work to be done as mutations. Inside a batch, calling `put`\nwith an entity, or `delete` with a key, builds up the request by\nadding a new mutation. This getter returns the protobuf that has been\nbuilt-up so far.\n\n### namespace\n\nGetter for namespace in which the batch will run.\n\n### project\n\nGetter for project in which the batch will run.\n\nMethods\n-------\n\n### begin\n\n begin()\n\nBegins a batch.\n\nThis method is called automatically when entering a with\nstatement, however it can be called explicitly if you don't want\nto use a context manager.\n\nOverridden by xref_Transaction.\n\n### commit\n\n commit(retry=None, timeout=None)\n\nCommits the batch.\n\nThis is called automatically upon exiting a with statement,\nhowever it can be called explicitly if you don't want to use a\ncontext manager.\n\n### current\n\n current()\n\nReturn the topmost batch / transaction, or None.\n\n### delete\n\n delete(key)\n\nRemember a key to be deleted during `commit`.\n\n### put\n\n put(entity)\n\nRemember an entity's state to be saved during `commit`.\n\n\u003cbr /\u003e\n\n| **Note:** Any existing properties for the entity will be replaced by those currently set on this instance. Already-stored properties which do not correspond to keys set on this instance will be removed from the datastore.\n| **Note:** Property values which are \"text\" ('unicode' in Python2, 'str' in Python3) map to 'string_value' in the datastore; values which are \"bytes\" ('str' in Python2, 'bytes' in Python3) map to 'blob_value'.\nWhen an entity has a partial key, calling `commit` sends it as an `insert` mutation and the key is completed. On return, the key for the `entity` passed in is updated to match the key ID assigned by the server.\n\n\u003cbr /\u003e\n\n### rollback\n\n rollback()\n\nRolls back the current batch.\n\nMarks the batch as aborted (can't be used again).\n\nOverridden by xref_Transaction."]]