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

# How to Defer Commands

> Client language: Python. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/how-to/defer-operations.md), [Java](https://docs.ravendb.net/6.2/client-api/session/how-to/defer-operations.java.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."

> **Note**
>
> - The `defer` method allows you to register server commands via the session.
>
> - All the deferred requests will be stored in the session and sent to the server
>   in a single batch when `save_changes` is called, along with any other changes/operations
>   made on the session.  
>   Thus, all deferred commands are **executed as part of the session's `save_changes` transaction**.
>
> - In this page:
>   - [Defer commands example](#defer-commands-example)
>   - [Commands that can be deferred](#commands-that-can-be-deferred)
>   - [Syntax](#syntax)

## Defer commands example

```python
# Defer is available in the session's advanced methods
session.advanced.defer(
  # Define commands to be executed:
  # i.e. Put a new document
  PutCommandDataBase(
      "products/999-A",
      None,
      {"Name": "My Product", "Supplier": "suppliers/1-A", "@metadata": {"@collection": "Products"}},
  ),
  # Patch document
  PatchCommandData("products/999-A", None, PatchRequest("this.Supplier = 'suppliers/2-A'"), None),
  # Force a revision to be created
  ForceRevisionCommandData("products/999-A"),
  # Delete a document
  DeleteCommandData("products/1-A", None),
)

# All deferred commands will be sent to the server upon calling save_changes
session.save_changes()
```

## Commands that can be deferred

The following commands implement the `ICommandData` interface and 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)
- BatchPatchCommandData
- 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)
- PutCompareExchangeCommandData
- DeleteCompareExchangeCommandData
- CopyTimeSeriesCommandData
- TimeSeriesBatchCommandData
- ForceRevisionCommandData

## Syntax

```python
def defer(self, *commands: CommandData) -> None: ...
```

| Parameter | Type | Description |
| - | - | - |
| **\*commands** | `CommandData` | An array of commands to be executed |

## 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.python.md)
- [Storing Entities](https://docs.ravendb.net/6.2/client-api/session/storing-entities.python.md)
- [Saving Changes](https://docs.ravendb.net/6.2/client-api/session/saving-changes.python.md)
