---
title: "How to Clear a Session"
url: "https://docs.ravendb.net/6.2/client-api/session/how-to/clear-a-session"
version: "6.2"
language: "nodejs"
---

# How to Clear a Session

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/how-to/clear-a-session.md), [Java](https://docs.ravendb.net/6.2/client-api/session/how-to/clear-a-session.java.md), [Python](https://docs.ravendb.net/6.2/client-api/session/how-to/clear-a-session.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/session/how-to/clear-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**
>
> - Use the `clear()` method to clear the session’s state:  
>   it **removes ALL tracked entities** and **cancels ALL pending operations** (e.g., *store*, *delete*).  
>   This is useful when you need to discard all tracked changes and reset the session state.
>
> - To remove only a **single entity** from tracking, see [Evict a single entity](https://docs.ravendb.net/6.2/client-api/session/how-to/evict-entity-from-a-session.nodejs.md).
>
> - In this article:
>   - [Clear the session's state](#clear-the-sessions-state)
>   - [Syntax](#syntax)

---

## Clear the session's state

```javascript
const session = store.openSession();
    
// Store a new employee
class Employee {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}
const newEmployee = new Employee("John", "Doe");
await session.store(newEmployee, "employees/1");    
    
// Load an existing employee and modify it
const existingEmployee = await session.load("employees/2");
existingEmployee.lastName = "UpdatedLastName";

// Load another employee and mark for deletion
const employeeToDelete = await session.load("employees/3");
session.delete(employeeToDelete);

// At this point:
// * employees/1 is pending insert
// * employees/2 is pending update
// * employees/3 is pending delete

// Clear the session state 
// this will remove all tracking and pending operations
session.advanced.clear();

// saveChanges does nothing - all operations were discarded
await session.saveChanges();
```

## Syntax

```javascript
session.advanced.clear();
```

## 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)
- [Evict Entity From a Session](https://docs.ravendb.net/6.2/client-api/session/how-to/evict-entity-from-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)
