---
title: "Evict a Single Entity from a Session"
url: "https://docs.ravendb.net/6.2/client-api/session/how-to/evict-entity-from-a-session"
version: "6.2"
language: "nodejs"
---

# Evict a Single Entity from a Session

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/how-to/evict-entity-from-a-session.md), [Java](https://docs.ravendb.net/6.2/client-api/session/how-to/evict-entity-from-a-session.java.md), [Python](https://docs.ravendb.net/6.2/client-api/session/how-to/evict-entity-from-a-session.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/session/how-to/evict-entity-from-a-session.php.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, the [session](https://docs.ravendb.net/6.2/client-api/session/what-is-a-session-and-how-does-it-work.nodejs.md) tracks all entities that it has either stored, loaded, or queried.  
>   Any changes made to these entities are automatically persisted when `saveChanges` is called.
>
> - Use the `evict` method to **remove a specific entity** from the session's tracking before calling *saveChanges*.  
>   eviction stops the session from tracking the entity, removes it from the identity map and internal cache,  
>   and cancels any pending operations for that entity (e.g., Store, Delete).
>
> - This article explains how to **evict a single entity** from the session.  
>   To evict *all* tracked entities and clear *all* pending operations, use the [clear](https://docs.ravendb.net/6.2/client-api/session/how-to/clear-a-session.nodejs.md) method instead.  
>   To prevent an entity from being tracked, see [DisableTracking](https://docs.ravendb.net/6.2/client-api/session/configuration/how-to-disable-tracking.nodejs.md).
>
> - In this article:
>   - [Evict an entity before saving](#evict-an-entity-before-saving)
>   - [Evict an entity to force reload from server](#evict-an-entity-to-force-reload-from-server)
>   - [Evict an entity from the session's delete queue](#evict-an-entity-from-the-sessions-delete-queue)
>   - [Limitations](#limitations)
>   - [Syntax](#syntax)

---

## Evict an entity before saving

- Calling `evict` on an entity that is *stored* removes it from the session’s tracking and cancels the pending store operation.  
  As a result, the entity will not be saved to the database when *saveChanges* is called.
- This can be useful if you’ve staged an entity to be stored, but then decide to discard it before committing changes.

```javascript
class Employee {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
} 
 
const session = store.openSession();

const employee1 = new Employee("John", "Doe");
const employee2 = new Employee("Jane", "Doe");

// Store both employees in the session
// The session begins tracking both employee entities
await session.store(employee1, "employees/1");
await session.store(employee2, "employees/2");

// Evict employee1
// This removes employee1 from the session's tracking 
// and cancels its pending store operation 
session.advanced.evict(employee1);

// Only employee2 will be saved
await session.saveChanges();
```

## Evict an entity to force reload from server

- Calling `evict` on an entity that was previously loaded removes it from the session’s tracking.  
  The next time you call *load* for that entity, RavenDB will fetch it from the server instead of returning the cached version.
- This can be useful if you want to ensure you're working with the latest version of a document,  
  for example, if you expect the document was modified outside the session.

```javascript
const session = store.openSession();`

// Load an entity from the server
// The session begins tracking the loaded entity
let employee = await session.load("employees/1");
console.log("number of requests = ", session.advanced.numberOfRequests); // 1

// Loading the same entity again does NOT trigger a server call
employee = await session.load("employees/1");
console.log("number of requests = ", session.advanced.numberOfRequests); // 1

// Evict the entity
// This removes it from the session's tracking
session.advanced.evict(employee); 

// Loading the entity now DOES trigger a server call
employee = await session.load("employees/1");
console.log("number of requests = ", session.advanced.numberOfRequests); // 2
```

## Evict an entity from the session's delete queue

- Calling `evict` on an entity that was previously marked for deletion removes it from the session's delete queue.  
  As a result, the deletion will not be sent to the server when *saveChanges* is called.
- This can be useful if you need to revert a pending deletion before committing changes.

```javascript
const session = store.openSession();

const employee1 = await session.load("employees/1");
const employee2 = await session.load("employees/2");

// Mark both employees for deletion
session.delete(employee1);
session.delete(employee2);

// Remove employee1 from tracking and from delete queue
session.advanced.evict(employee1); 

// Only employee2 will be deleted
await session.saveChanges();
```

## Limitations

- `evict` cannot be called from within an [beforeStore](https://docs.ravendb.net/6.2/client-api/session/how-to/subscribe-to-events.nodejs.md#onbeforestore) or
  [beforeDelete](https://docs.ravendb.net/6.2/client-api/session/how-to/subscribe-to-events.nodejs.md#onbeforedelete) event handler.  
  Attempting to do so will throw an exception.
- This is a design limitation intended to prevent changes to the session's internal state during event handlers that are triggered by *saveChanges*.
  For example:

```javascript
{
    const session = store.openSession();
    await session.store(new Employee("John", "Doe"), "employees/1");
    await session.store(new Employee("Jane", "Doe"), "employees/2");
    await session.saveChanges();
}
    
{
    // Register event handler for 'beforeStore'
    store.addSessionListener("beforeStore", async (args) => {
        try {
            const session = args.session;
            const employee2 = await session.load("employees/2");
            session.advanced.evict(employee2);
        } catch (e) {
            console.log("OnBeforeStore exception:", e.message);
            // Expected: "Cannot Evict entity during OnBeforeStore..."
        }
    });
    
    // Register event handler for 'beforeDelete'
    store.addSessionListener("beforeDelete", async (args) => {
        try {
            const session = args.session;
            const employee2 = await session.load("employees/2");
            session.advanced.evict(employee2);
        } catch (e) {
            console.log("OnBeforeDelete exception:", e.message);
            // Expected: "Cannot Evict entity during OnBeforeDelete..."
        }
    });
}

// Try evicting in beforeStore
{
    const session = store.openSession();
    await session.store({ firstName: "Foo3" }, "employees/3");
    
    await session.saveChanges(); // Triggers beforeStore
}

// Try evicting in beforeDelete
{
    const session = store.openSession();
    const employee1 = await session.load("employees/1");
    session.delete(employee1);
    
    await session.saveChanges(); // Triggers beforeDelete
}
```

## Syntax

```javascript
session.advanced.evict(entity);
```

| Parameter | Type | Description |
| - | - | - |
| **entity** | object | Instance of the entity to evict from tracking |

## 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.nodejs.md)
- [Clear a Session](https://docs.ravendb.net/6.2/client-api/session/how-to/clear-a-session.nodejs.md)
- [Refresh Entity](https://docs.ravendb.net/6.2/client-api/session/how-to/refresh-entity.nodejs.md)
- [How to Check if Entity has Changed](https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-entity-has-changed.nodejs.md)
