---
title: "Conflict Resolution"
description: "* Define the server behaviour upon a conflict between documents."
url: "https://docs.ravendb.net/6.2/studio/database/settings/conflict-resolution"
version: "6.2"
---

# Conflict Resolution

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

## Conflict Resolution

> **Note**
>
> - Define the server behaviour upon a conflict between documents.
>
> - Conflicts that are created can be manually resolved in [Documents Conflicts View](https://docs.ravendb.net/6.2/studio/database/documents/conflicts-view.md).
>
> - In this page:
>   - [The Conflicts Resolution View](#the-conflicts-resolution-view)
>   - [Script Examples](#script-examples)

## The Conflicts Resolution View

![Figure 1. Conflicts Resolution View](https://docs.ravendb.net/assets/images/conflict-resolution-view-1-8dcc42f800c4b700b1c38badbdb94671.png)

1. **Save, Add, Modify or Delete** conflict resolution scripts.

2. **Set default behaviour**  -  Conflicts will be created *only if*:
   - This option is unchecked and no script is defined
   - This option is unchecked and the script is defined but returns null

3. **Resolution Script** (optional)
   - Supply a javaScript function to resolve the conflicting documents.

   - The object that is returned by the script will be used as the conflict resolution in the document.

   - The sript is defined per collection

   - Note: in case the script returns null (either intentionally, or upon some error),  
     the server will resolve the conflict according to the defined default behaviour  
     (i.e. resolve by using latest version or creating a conflict for the user to resolve).

   - Script Variables:
     - ***docs*** - the conflicted documents objects array
     - ***hasTombstone*** - true if either of the conflicted documents was deleted
     - ***resolveToTombstone*** - return this value from script if the resolution wanted is to delete this document

## Script Examples

> **Note**
>
> - 1. Resolve according to field content - return the highest value of the field
>
> ```javascript
> // First conflicting document
> {
>   "Name": "John",
>   "MaxRecord": 43
> }
> ```
>
> ```javascript
> // Second conflicting document
> {
>   "Name": "John",
>   "MaxRecord": 80
> }
> ```
>
> ```javascript
> // The resolving script:
> var maxRecord = 0;
> for (var i = 0; i < docs.length; i++) {
>   maxRecord = Math.max(docs[i].maxRecord, maxRecord);
> }
> docs[0].MaxRecord = maxRecord;
>
> return docs[0];
> ```

> **Note**
>
> - 2. Resolve by deleting the document
>
> ```javascript
> if (hasTombstone) {
>   return resolveToTombstone;
> }
> ```

> **Note**
>
> - 3. The metadata can also be accessed - return the document that has the largest number of attachments
>
> ```javascript
> var result = docs[0];
>
> for (var i = 1; i < docs.length; i++) {
>    if (docs[i]["@metadata"]["@attachments"].length > result["@metadata"]["@attachments"].length)
>       result = docs[i];
>   }
> }
>
> return result;
> ```
