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

# Disable Entity Tracking

> Client language: PHP. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/configuration/how-to-disable-tracking.md), [Python](https://docs.ravendb.net/6.2/client-api/session/configuration/how-to-disable-tracking.python.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 `saveChanges` 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 `ignoreChangesFor`.
- Once changes are ignored for the entity:
  - Any modifications made to the entity will be ignored by `saveChanges`.
  - 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**

```php
// Load a product entity - the session will track the entity by default
/** @var Product $product */
$product = $session->load(Product::class, "products/1-A");

// Call 'ignoreChangesFor' to instruct the session to ignore changes made to this entity
$session->advanced()->ignoreChangesFor($product);

// The following change will be ignored by saveChanges - it will not be persisted
$product->setUnitsInStock($product->getUnitsInStock() + 1);

$session->saveChanges();
```

## 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.

```php
$sessionOptions = new SessionOptions();
// Disable tracking for all entities in the session's options
$sessionOptions->setNoTracking(true);

$session = $store->openSession($sessionOptions);
try {
  // Load any entity, it will Not be tracked by the session
  /** @var Employee $employee1 */
  $employee1 = $session->load(Employee::class, "employees/1-A");

  // Loading again from same document will result in a new entity instance
  $employee2 = $session->load(Employee::class, "employees/1-A");

  // Entities instances are not the same
  $this->assertNotEquals($employee1, $employee2);
} finally {
  $session->close();
}
```

## Disable tracking for query results

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

### query

```php
$session = $store->openSession();
try {
  // Define a query
  /** @var array<Employee> $employeesResults */
  $employeesResults = $session->query(Employee::class)
      // Set NoTracking, all resulting entities will not be tracked
      ->noTracking()
      ->whereEquals("FirstName", "Robert")
      ->toList();

  // The following modification will not be tracked for SaveChanges
  $firstEmployee = $employeesResults[0];
  $firstEmployee->setLastName("NewName");

  // Change to 'firstEmployee' will not be persisted
  $session->saveChanges();
} finally {
  $session->close();
}
```

### documentQuery

```php
$session = $store->openSession();
try {
  // Define a query
  /** @var array<Employee> $employeesResults */
  $employeesResults = $session->advanced()->documentQuery(Employee::class)
      // Set NoTracking, all resulting entities will not be tracked
      ->noTracking()
      ->whereEquals("FirstName", "Robert")
      ->toList();

  // The following modification will not be tracked for SaveChanges
  $firstEmployee = $employeesResults[0];
  $firstEmployee->setLastName("NewName");

  // Change to 'firstEmployee' will not be persisted
  $session->saveChanges();
} finally {
  $session->close();
}
```

## Customize tracking in conventions

- You can further customize and fine-tune which entities will not be tracked  
  by configuring the `ShouldIgnoreEntityChanges` convention method on the document store.
- This customization will apply to all sessions opened for this document store.
- Use the `setShouldIgnoreEntityChanges` method to do so.

#### Example:

```php
// Define the 'ignore' convention on your document store
$conventions = new DocumentConventions();
$conventions->setShouldIgnoreEntityChanges(
// Define for which entities tracking should be disabled
// Tracking will be disabled ONLY for entities of type Employee whose FirstName is Bob
  function ($session, $entity, $id) {
      return $entity instanceof Employee && $entity->getFirstName() == "Bob";
  }
);

$store = new DocumentStore();
$store->setConventions($conventions);
$store->initialize();
try {
  $session = $store->openSession();
  try {
      $employee1 = new Employee();
      $employee1->setId("employees/1");
      $employee1->setFirstName("Alice");

      $employee2 = new Employee();
      $employee2->setId("employees/2");
      $employee2->setFirstName("Bob");

      $session->store($employee1);      // This entity will be tracked
      $session->store($employee2);      // Changes to this entity will be ignored

      $session->saveChanges();          // Only employee1 will be persisted

      $employee1->setFirstName("Bob");  // Changes to this entity will now be ignored
      $employee2->setFirstName("Alice");// This entity will now be tracked

      $session->saveChanges();          // Only employee2 is persisted
  } finally {
      $session->close();
  }
} finally {
  $store->close();
}
```

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