---
title: "Disable Entity Tracking"
url: "https://docs.ravendb.net/6.2/client-api/session/configuration/how-to-disable-tracking"
version: "6.2"
language: "python"
---

# Disable Entity Tracking

> Client language: Python. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/configuration/how-to-disable-tracking.md), [PHP](https://docs.ravendb.net/6.2/client-api/session/configuration/how-to-disable-tracking.php.md), [Node.js](https://docs.ravendb.net/6.2/client-api/session/configuration/how-to-disable-tracking.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**
>
> - By default, each session tracks changes to all entities it has either stored, loaded, or queried for.  
>   All changes are then persisted when `save_changes` is called.
>
> - Tracking can be disabled at various scopes:  
>   for a specific entity, for entities returned by a query, for all entities in a session, or globally using conventions.
>
> - In this article:
>   - [Disable tracking changes for a specific entity](#disable-tracking-changes-for-a-specific-entity)
>   - [Disable tracking all entities in session](#disable-tracking-all-entities-in-session)
>   - [Disable tracking query results](#disable-tracking-query-results)
>   - [Customize tracking in conventions](#customize-tracking-in-conventions)

## Disable tracking changes for a specific entity

- You can prevent the session from persisting changes made to a specific entity by using `ignore_changes_for`.
- Once changes are ignored for the entity:
  - Any modifications made to the entity will be ignored by `save_changes`.
  - The session will still keep a reference to the entity to avoid repeated server requests.  
    Performing another `load` for the same entity will Not generate another call to the server.

**Example**

```python
# Load a product entity - the session will track the entity by default
product = session.load("products/1-A", Product)

# Call 'ignore_changes_for' to instruct the session to ignore changes made to this entity
session.ignore_changes_for(product)

# The following change will be ignored by SaveChanges - it will not be persisted
product.units_in_stock += 1
session.save_changes()
```

**Syntax**

```python
def ignore_changes_for(self, entity: object) -> None: ...
```

| Parameter | Type | Description |
| - | - | - |
| **entity** | `object` | Instance of entity for which changes will be ignored |

## Disable tracking for all entities in a session

- Tracking can be disabled for all entities in the session's options.
- When tracking is disabled for the session:
  - Method `store` will Not be available (an exception will be thrown if used).
  - Calling `load` or `query` will generate a call to the server and create new entities instances.

```python
with store.open_session(
  SessionOptions(
      # Disable tracking for all entities in the session's options
      no_tracking=True
  )
):
  # Load any entity, it will Not be tracked by the session
  employee1 = session.load("employees/1-A", Employee)

  # Loading again from same document will result in a new entity instance
  employee2 = session.load("employees/1-A", Employee)

  # Entities instances are not the same
  self.assertNotEqual(employee1, employee2)
```

## Disable tracking for query results

- Tracking can be disabled for all entities resulting from a query.

```python
with store.open_session() as session:
  # Define a query
  employees_results = list(
      session.advanced.document_query(object_type=Employee)
      # Set no_tracking, all resulting entities will not ne tracked
      .no_tracking().where_equals("FirstName", "Robert")
  )

  # The following modification will not be tracked for save_changes
  first_employee = employees_results[0]
  first_employee.last_name = "NewName"

  # Change to 'first_employee' will not be persisted
  session.save_changes()
```

## Customize tracking in conventions

- You can further customize and fine-tune which entities will not be tracked  
  by configuring the `should_ignore_entity_changes` convention method on the document store.
- This customization rule will apply to all sessions opened for this document store.
- Implement rules under your [ShouldIgnoreEntityChanges](#syntax) subclass.  
  Apply the class's `check` method to control the ignore flow.

**Example**:

```python
with DocumentStore() as store:
  # Define the 'ignore' convention on your document store:

  # Create a class that implements 'ravendb.documents.conventions.ShouldIgnoreEntityChanges'
  # and implement 'check' method - it's going to be called to check if entity should be ignored

  class MyCustomShouldIgnoreEntityChanges(ShouldIgnoreEntityChanges):
      def check(self, session_operations: DocumentSession, entity: object, document_id: str) -> bool:
          # Define for which entities tracking should be disabled
          # Tracking will be disabled ONLY for entities of type Employee whose FirstName is Bob
          return isinstance(entity, Employee) and entity.first_name == "Bob"

  store.conventions.should_ignore_entity_changes = MyCustomShouldIgnoreEntityChanges

  store.initialize()

  with store.open_session() as session:
      employee1 = Employee(first_name="Alice", Id="employees/1")
      employee2 = Employee(first_name="Bob", Id="employees/2")

      session.store(employee1)  # This entity will be tracked
      session.store(employee2)  # Changes to this entity will be ignored

      session.save_changes()

      employee1.first_name = "Bob"  # Changes to this entity will now be ignored
      employee2.first_name = "Alice"  # This entity will now be tracked

      session.save_changes()
```

**Syntax**:

```python
@should_ignore_entity_changes.setter
def should_ignore_entity_changes(self, value: ShouldIgnoreEntityChanges) -> None: ...

class ShouldIgnoreEntityChanges(ABC):
  @abstractmethod
  def check(
      self,
      session_operations: "InMemoryDocumentSessionOperations",
      entity: object,
      document_id: str,
  ) -> bool:
      pass
```

| Parameter | Type | Description |
| - | - | - |
| entity | `object` | The entity for which tracking is to be disabled |
| document\_id | `str` | The entity's document ID |

| Return Type | Description |
| - | - |
| `bool` | `True` - Entity will Not be tracked<br />`False` - Entity will be tracked |

## See also

- [Document Store Conventions](https://docs.ravendb.net/6.2/client-api/configuration/conventions.md)
- [How to Ignore Entity Changes](https://docs.ravendb.net/6.2/client-api/session/how-to/ignore-entity-changes.python.md)
- [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)
- [Opening a Session](https://docs.ravendb.net/6.2/client-api/session/opening-a-session.python.md)
- [Storing Entities](https://docs.ravendb.net/6.2/client-api/session/storing-entities.python.md)
- [Loading Entities](https://docs.ravendb.net/6.2/client-api/session/loading-entities.python.md)
- [Saving Changes](https://docs.ravendb.net/6.2/client-api/session/saving-changes.python.md)
