Document Refresh: Client API
-
The document refresh feature re-saves an existing document once the time you schedule for it has passed, without changing the document's data.
Re-saving the document triggers features that react to document updates, like indexing, ETL tasks, and data subscriptions.
Use document refresh for documents that need to be processed at a later time, like documents that hold emails to send on a set date, room reservations to release once they expire, or reminders to raise when they are due. -
To schedule a document's refresh, set the
@refreshmetadata property of the document to the intended refresh time.
The@refreshproperty is removed when the document is re-saved. -
To enable document refresh for a database, send the server a
RefreshConfigurationobject using the ConfigureRefreshOperation operation.
TheRefreshConfigurationobject also sets the frequency at which the document refresh feature checks the database for documents to refresh. -
To enable and configure document refresh using Studio, see Document Refresh: Studio View.
-
In this article:
Refreshing documents
Using document refresh, you can schedule RavenDB to re-save a document at a set time.
This way, features that react to document changes (e.g., indexes and ETL tasks) process the document once the set time has
passed, with no further action required from your application.
For example:
- An email that should be sent on a set date.
- A room reservation that should be released once it expires.
- A reminder that should be raised when it is due.
To have a document processed only once its refresh time has passed, make the subscription query, index definition, or ETL
script that handles the document skip documents whose metadata still holds the @refresh property.
e.g., a data subscription with the query from Emails as e where true and not exists(e.'@metadata'.'@refresh') sends an
email document to its worker only after the document is refreshed.
The refresh process
-
Enabling document refresh for a database:
Document refresh is disabled by default. See Enabling document refresh below. -
Scheduling a document's refresh:
Add a@refreshproperty to the metadata of the document you want to refresh, and set the property's value to the intended refresh time.
e.g.,"@refresh": "2026-12-01T08:00:00.0000000Z"
See Scheduling a document refresh below. -
Refreshing the scheduled document:
The document refresh feature runs periodically (every 60 seconds by default).
Each time the feature runs, it checks the database for documents to refresh.- In the feature's first run after a document's refresh time, the
@refreshproperty is removed from the document's metadata, and the modified document is stored.
As a result, the document's change vector is incremented, indicating that the document was updated. - The document may be refreshed in a later run if the number of documents due for refresh exceeds the maximum set for a single run by MaxItemsToProcess.
- In the feature's first run after a document's refresh time, the
-
Processing the refreshed document:
Features that react to document changes are triggered by this update and process the refreshed document.
For example:- The document is re-indexed by the indexes defined for the document's collection.
- The document is replicated to the other nodes of the database group, and to the destinations of external replication tasks.
- An ETL task whose transformation script handles the document's collection runs the script over the document.
- A data subscription whose query matches the updated document sends the document to the subscription's worker (or to one of its workers, for a concurrent subscription).
- If revisions are enabled for the document's collection, a new revision of the document is created.
-
Rescheduling the document's refresh:
Since the@refreshproperty is removed, the document is refreshed only once.
To refresh the document again, set the document's@refreshmetadata property to a new time.
Enabling document refresh
To enable document refresh for a database, send the server a
RefreshConfiguration object with its Disabled property
set to false, using the
ConfigureRefreshOperation operation.
e.g., the following code enables document refresh, sets the feature to run every five minutes, and limits the number of documents processed in each run to 1,000.
// Define the refresh configuration
var refreshConfiguration = new RefreshConfiguration
{
// Enable document refresh for the database
Disabled = false,
// Check the database for documents to refresh every 5 minutes
RefreshFrequencyInSec = 300,
// Process up to 1,000 documents in each run
MaxItemsToProcess = 1000
};
// Send the configuration to the server
store.Maintenance.Send(new ConfigureRefreshOperation(refreshConfiguration));
On a secure server, enabling or configuring the document refresh feature requires, like other changes to a database's
configuration, a client certificate with Admin access to the database (or with an Operator or Cluster Admin
security clearance).
Setting a document's @refresh metadata property, on the other hand, is a change to the document, which Read/Write access
permits.
See Setting up Authentication and Authorization
The RefreshConfiguration object holds the following settings:
-
DisabledSet this property to
falseto enable document refresh, or totrueto disable the feature.
The default value for this property isfalse, so sending a newRefreshConfigurationobject enables document refresh unless you explicitly setDisabledtotrue. -
RefreshFrequencyInSecSets the interval, in seconds, between the document refresh feature's checks for documents to refresh.
The default value for this property is 60 seconds.-
The
RefreshFrequencyInSecvalue must be greater than 0:
Sending a value of 0 or less fails with aRavenException, whose inner exception is anInvalidOperationException.
e.g., the following code sends aRefreshFrequencyInSecvalue of 0 and catches the resulting exception.try{store.Maintenance.Send(new ConfigureRefreshOperation(new RefreshConfiguration{Disabled = false,RefreshFrequencyInSec = 0}));}catch (RavenException e) when (e.InnerException is InvalidOperationException){// The inner exception's message includes:// "Refresh frequency for database '<database name>' must be greater than 0."} -
On a Community license, the
RefreshFrequencyInSecvalue must be at least 129600 (36 hours):
Enabling document refresh with a lower value, or without settingRefreshFrequencyInSec(so that the 60-second default applies), fails with aLicenseLimitException.
e.g., the code at the beginning of this section, which setsRefreshFrequencyInSecto 300, fails on a Community license.
-
-
MaxItemsToProcessSets the maximum number of documents the document refresh feature processes in a single run (each time the feature checks the database for documents whose refresh time has passed).
By default, a single run can process any number of documents.- Each run processes the documents due for refresh in the order of their refresh times, earliest first.
Documents that share a refresh time are processed in the order of their document IDs. - If the number of documents due for refresh exceeds the set maximum, the documents will be processed over multiple consecutive runs.
- Each run processes the documents due for refresh in the order of their refresh times, earliest first.
You can also enable document refresh when you create a database, by setting the Refresh property of the database record
to a RefreshConfiguration object.
See Create Database Operation.
Scheduling a document refresh
To schedule a document's refresh, set the @refresh metadata property of the document to the intended refresh time, and save
the document.
The @refresh metadata property is reserved for the document refresh feature.
Do not use it for any other purpose: RavenDB reads any value set for this property as a refresh time.
A valid date and time schedules the document's refresh, and any other value makes saving the document fail.
e.g., the following code schedules a refresh of the users/1-A document for one hour from now.
using (var session = store.OpenSession())
{
// Load the document
var user = session.Load<User>("users/1-A");
// Set the document's refresh time to one hour from now, in UTC
session.Advanced.GetMetadataFor(user)["@refresh"] = DateTime.UtcNow.AddHours(1);
// Save the document
session.SaveChanges();
}
-
Setting the refresh time in UTC
Set the
@refreshproperty to a UTCDateTimevalue (e.g.,DateTime.UtcNow.AddHours(1)).
The client sends a UTC value with aZsuffix, which marks the time as UTC, so the server reads the intended time correctly.
e.g.,"@refresh": "2026-12-01T08:00:00.0000000Z"Do not set the property to a local
DateTimevalue (e.g.,DateTime.Now.AddHours(1)).
If you set a localDateTimevalue, the client will send the value without a time zone designator, and the server will read the value as the server machine's local time. If the client machine and the server machine are in different time zones, the document will be refreshed at a different time than intended. -
Setting an invalid refresh time
If the value of the
@refreshproperty is not a valid date and time,SaveChangesthrows aRavenException, and none of the session's changes are saved. -
Scheduling while document refresh is disabled
Setting the
@refreshproperty while document refresh is disabled for the database still schedules the document's refresh:- If you enable the feature before the refresh time, the document will be refreshed in the feature's first run after the refresh time.
- If the refresh time passes while the feature is disabled, the document will be refreshed after the feature is enabled, during its first run.
- In both cases, the document may be refreshed in a later run if the number of documents due for refresh exceeds the maximum set for a single run by MaxItemsToProcess.
-
Conflicted documents
A document that is in conflict is not refreshed while the conflict remains, even if the refresh time of all its conflicting versions has passed.
- If the document that resolves the conflict holds a
@refreshproperty whose time has passed, the document will be refreshed in the feature's first run after the conflict is resolved. - A document remains in conflict only if the conflict is not resolved automatically: by default, RavenDB resolves conflicts to the most recently modified version of the document (see Conflict Detection).
- If the document that resolves the conflict holds a
-
Changing or canceling a scheduled refresh
To change a document's refresh time, set its
@refreshproperty to a new time and save the document.
To cancel the refresh, remove the@refreshproperty from the document's metadata and save the document.
e.g.,session.Advanced.GetMetadataFor(user).Remove("@refresh"); -
Reverting documents to revisions
When you revert a document to a revision (or revert documents to a specific time), the reverted document gets the content of the revision without the revision's
@refreshproperty, even if the revision holds this property.- The
@refreshproperty is omitted from the reverted document because the revision's refresh time was set when the revision was created, and has usually already passed by the time of the revert.
Keeping the property would refresh the reverted document right after the revert. - Since the
@refreshproperty is omitted, the reverted document will no longer be scheduled for a refresh unless you set its@refreshproperty again.
- The
Syntax
ConfigureRefreshOperation
Sends a RefreshConfiguration object to the server, to enable or disable document refresh for a database and configure the
feature.
public ConfigureRefreshOperation(RefreshConfiguration configuration)
Usage:
// Sync
ConfigureRefreshOperationResult result =
store.Maintenance.Send(new ConfigureRefreshOperation(refreshConfiguration));
// Async
ConfigureRefreshOperationResult asyncResult =
await store.Maintenance.SendAsync(new ConfigureRefreshOperation(refreshConfiguration));
| Parameter | Type | Description |
|---|---|---|
| configuration | RefreshConfiguration | The document refresh configuration to apply to the database. |
| Return value | |
|---|---|
| ConfigureRefreshOperationResult | Contains RaftCommandIndex. |
Classes
- RefreshConfiguration
- ConfigureRefreshOperationResult
RefreshConfiguration
Defines the document refresh configuration of a database.
class RefreshConfiguration
{
bool Disabled
long? RefreshFrequencyInSec
long? MaxItemsToProcess
}
| Property | Type | Description |
|---|---|---|
| Disabled | bool | Set to false to enable document refresh for the database, or to true to disable the feature.The default value is false. |
| RefreshFrequencyInSec | long? | The interval, in seconds, between the feature's checks for documents to refresh. The value must be greater than 0. If null, the default value of 60 seconds applies.On a Community license, the value must be at least 129600 (36 hours). See Enabling document refresh. |
| MaxItemsToProcess | long? | The maximum number of documents the feature processes in a single run. If null, a single run can process any number of documents. |
ConfigureRefreshOperationResult
The result of sending a ConfigureRefreshOperation operation to the server.
class ConfigureRefreshOperationResult
{
long? RaftCommandIndex
}
| Property | Type | Description |
|---|---|---|
| RaftCommandIndex | long? | The Raft index of the command that applied the configuration. |