---
title: "How to Defer Commands"
url: "https://docs.ravendb.net/6.2/client-api/session/how-to/defer-operations"
version: "6.2"
language: "java"
---

# How to Defer Commands

> Client language: Java. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/how-to/defer-operations.md), [Python](https://docs.ravendb.net/6.2/client-api/session/how-to/defer-operations.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/session/how-to/defer-operations.php.md), [Node.js](https://docs.ravendb.net/6.2/client-api/session/how-to/defer-operations.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

Commands can be deferred till `saveChanges` is called by using `defer` method in `advanced` session operations.  
All of the operations will update teh session state appropriately after `saveChanges` is called.

Types of commands that can be deferred:

- [PutCommandData](https://docs.ravendb.net/6.2/glossary/put-command-data.md)
- [DeleteCommandData](https://docs.ravendb.net/6.2/glossary/delete-command-data.md)
- DeletePrefixedCommandData
- [PatchCommandData](https://docs.ravendb.net/6.2/glossary/patch-command-data.md)
- PutAttachmentCommandData
- DeleteAttachmentCommandData
- [CopyAttachmentCommandData](https://docs.ravendb.net/6.2/glossary/copy-attachment-command-data.md)
- [MoveAttachmentCommandData](https://docs.ravendb.net/6.2/glossary/move-attachment-command-data.md)
- [CountersBatchCommandData](https://docs.ravendb.net/6.2/glossary/counters-batch-command-data.md)

## Syntax

```java
void defer(ICommandData command, ICommandData... commands);

void defer(ICommandData[] commands);
```

| Parameter | Description |
| - | - |
| `ICommandData` | Command to be executed. |
| `ICommandData[]` | Array of commands implementing `ICommandData` interface. |

## Example

```java
Map<String, Object> value1 = new HashMap<>();
value1.put("Name", "My Product");
value1.put("Supplier", "suppliers/999-A");
value1.put("@metadata", Collections.singletonMap("@collection", "Users"));

PutCommandDataWithJson putCommand1 =
  new PutCommandDataWithJson("products/999-A",
      null,
      store.getConventions().getEntityMapper().valueToTree(value1));

HashMap<String, Object> value2 = new HashMap<>();
value2.put("Name", "My Product");
value2.put("Supplier", "suppliers/999-A");
value2.put("@metadata", Collections.singletonMap("@collection", "Suppliers"));

PutCommandDataWithJson putCommand2 =
  new PutCommandDataWithJson("suppliers/999-A",
      null,
      store.getConventions().getEntityMapper().valueToTree(value2));

DeleteCommandData command3 = new DeleteCommandData("products/1-A", null);

session.advanced().defer(putCommand1, putCommand2, command3);
```

## See also

- [What is a Session and How Does it Work](https://docs.ravendb.net/6.2/client-api/session/what-is-a-session-and-how-does-it-work.java.md)
- [Storing Entities](https://docs.ravendb.net/6.2/client-api/session/storing-entities.java.md)
- [Saving Changes](https://docs.ravendb.net/6.2/client-api/session/saving-changes.java.md)
