WriteBatch

public classWriteBatch


A write batch, used to perform multiple writes as a single atomic unit.

A Batch object can be acquired by callingbatch. It provides methods for adding writes to the write batch. None of the writes will be committed (or visible locally) untilcommit is called.

Unlike transactions, write batches are persisted offline and therefore are preferable when you don't need to condition your writes on read data.

Subclassing Note: Cloud Firestore classes are not meant to be subclassed except for use in test mocks. Subclassing is not supported in production code and new SDK releases may break code that does so.

Summary

Nested types

public interfaceWriteBatch.Function

An interface for providing code to be executed within aWriteBatch context.

Public methods

@NonNullTask<Void>

Commits all of the writes in this write batch as a single atomic unit.

@NonNullWriteBatch

Deletes the document referred to by the providedDocumentReference.

@NonNullWriteBatch

Overwrites the document referred to by the providedDocumentReference.

@NonNullWriteBatch
set(
    @NonNullDocumentReference documentRef,
    @NonNullObject data,
    @NonNullSetOptions options
)

Writes to the document referred to by the providedDocumentReference.

@NonNullWriteBatch
update(
    @NonNullDocumentReference documentRef,
    @NonNullMap<StringObject> data
)

Updates fields in the document referred to by the providedDocumentReference.

@NonNullWriteBatch
update(
    @NonNullDocumentReference documentRef,
    @NonNullString field,
    @NullableObject value,
    Object[] moreFieldsAndValues
)

Updates field in the document referred to by the providedDocumentReference.

@NonNullWriteBatch
update(
    @NonNullDocumentReference documentRef,
    @NonNullFieldPath fieldPath,
    @NullableObject value,
    Object[] moreFieldsAndValues
)

Updates fields in the document referred to by the providedDocumentReference.

Public methods

commit

public @NonNullTask<Voidcommit()

Commits all of the writes in this write batch as a single atomic unit.

Returns
@NonNullTask<Void>

A Task that will be resolved when the write finishes.

delete

public @NonNullWriteBatch delete(@NonNullDocumentReference documentRef)

Deletes the document referred to by the providedDocumentReference.

Parameters
@NonNullDocumentReference documentRef

TheDocumentReference to delete.

Returns
@NonNullWriteBatch

ThisWriteBatch instance. Used for chaining method calls.

set

public @NonNullWriteBatch set(@NonNullDocumentReference documentRef, @NonNullObject data)

Overwrites the document referred to by the providedDocumentReference. If the document does not yet exist, it will be created. If a document already exists, it will be overwritten.

Parameters
@NonNullDocumentReference documentRef

TheDocumentReference to overwrite.

@NonNullObject data

The data to write to the document (like a Map or a POJO containing the desired document contents).

Returns
@NonNullWriteBatch

ThisWriteBatch instance. Used for chaining method calls.

set

public @NonNullWriteBatch set(
    @NonNullDocumentReference documentRef,
    @NonNullObject data,
    @NonNullSetOptions options
)

Writes to the document referred to by the providedDocumentReference. If the document does not yet exist, it will be created. If you passSetOptions, the provided data can be merged into an existing document.

Parameters
@NonNullDocumentReference documentRef

TheDocumentReference to overwrite.

@NonNullObject data

The data to write to the document (like a Map or a POJO containing the desired document contents).

@NonNullSetOptions options

An object to configure the set behavior.

Returns
@NonNullWriteBatch

ThisWriteBatch instance. Used for chaining method calls.

update

public @NonNullWriteBatch update(
    @NonNullDocumentReference documentRef,
    @NonNullMap<StringObject> data
)

Updates fields in the document referred to by the providedDocumentReference. If no document exists yet, the update will fail.

Parameters
@NonNullDocumentReference documentRef

TheDocumentReference to update.

@NonNullMap<StringObject> data

A map of field / value pairs to update. Fields can contain dots to reference nested fields within the document.

Returns
@NonNullWriteBatch

ThisWriteBatch instance. Used for chaining method calls.

update

public @NonNullWriteBatch update(
    @NonNullDocumentReference documentRef,
    @NonNullString field,
    @NullableObject value,
    Object[] moreFieldsAndValues
)

Updates field in the document referred to by the providedDocumentReference. If no document exists yet, the update will fail.

Parameters
@NonNullDocumentReference documentRef

TheDocumentReference to update.

@NonNullString field

The first field to update. Fields can contain dots to reference a nested field within the document.

@NullableObject value

The first value

Object[] moreFieldsAndValues

Additional field/value pairs.

Returns
@NonNullWriteBatch

ThisWriteBatch instance. Used for chaining method calls.

update

public @NonNullWriteBatch update(
    @NonNullDocumentReference documentRef,
    @NonNullFieldPath fieldPath,
    @NullableObject value,
    Object[] moreFieldsAndValues
)

Updates fields in the document referred to by the providedDocumentReference. If no document exists yet, the update will fail.

Parameters
@NonNullDocumentReference documentRef

TheDocumentReference to update.

@NonNullFieldPath fieldPath

The first field to update.

@NullableObject value

The first value

Object[] moreFieldsAndValues

Additional field/value pairs.

Returns
@NonNullWriteBatch

ThisWriteBatch instance. Used for chaining method calls.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-21 UTC.