Skip to main content

Delete Revisions Operation

Delete all revisions - single document

In this example, we delete ALL revisions of a document.
Both types of revisions, those resulting from the revisions settings and those generated manually via
force revision creation, will be deleted.

// Define the delete revisions operation:

// Delete ALL existing revisions for document "orders/830-A"
var deleteRevisionsOp = new DeleteRevisionsOperation(documentId: "orders/830-A",
// Revisions that were created manually will also be removed
removeForceCreatedRevisions: true);

// Execute the operation by passing it to Maintenance.Send
var numberOfRevisionsDeleted = store.Maintenance.Send(deleteRevisionsOp);

// Running the above code on RavenDB's sample data results in the removal of 29 revisions
Assert.Equal(29, numberOfRevisionsDeleted.TotalDeletes);

Delete revisions - multiple documents

You can specify multiple documents from which to delete revisions.

// Delete existing revisions for the specified documents
var deleteRevisionsOp = new DeleteRevisionsOperation(
documentIds: new List<string>() { "orders/829-A", "orders/828-A", "orders/827-A" },
// Revisions that were created manually will Not be removed
removeForceCreatedRevisions: false);

var numberOfRevisionsDeleted = store.Maintenance.Send(deleteRevisionsOp);

// Running the above on RavenDB's sample data results in the removal of 19 revisions
Assert.Equal(19, numberOfRevisionsDeleted.TotalDeletes);

Delete revisions by time frame

You can specify a time frame from which to delete revisions.
Only revisions that were created within that time frame (inclusive) will be deleted.
The time should be specified in UTC.

var deleteFrom = DateTime.Parse("2018-07-27T09:11:52.0Z");
var deleteTo = DateTime.Parse("2018-07-27T09:11:54.0Z");

// Delete existing revisions within the specified time frame
var deleteRevisionsOp =
new DeleteRevisionsOperation(documentId: "orders/826-A", from: deleteFrom, to: deleteTo);

var numberOfRevisionsDeleted = store.Maintenance.Send(deleteRevisionsOp);

Delete revisions by change vectors

Each revision has its own unique change vector.
You can specify which revisions to delete by providing their corresponding change vectors.
No exception is thrown if a change vector doesn’t match any revision.

// Get the change-vectors for the revisions of the specified document
var revisionsChangeVectors = session.Advanced.Revisions
.GetMetadataFor("orders/825-A")
.Select(m => m.GetString(Constants.Documents.Metadata.ChangeVector))
.ToList();

// Delete the revisions by their change-vector
var revisionToDelete =
new List<string>() { revisionsChangeVectors[0], revisionsChangeVectors[1] };

var deleteRevisionsOp =
new DeleteRevisionsOperation(documentId: "orders/825-A", revisionToDelete);

var numberOfRevisionsDeleted = store.Maintenance.Send(deleteRevisionsOp);

Avoid deleting a "Delete Revision" using the DeleteRevisionsOperation operation.
Consider the following scenario:

  1. A document that has revisions is deleted.

  2. A "Delete Revision" is created for the document, and it will be listed in the Revisions Bin.

  3. The revisions of this deleted document remain accessible via the Revisions Bin.

  4. If you remove this "Delete Revision" by providing its change vector to DeleteRevisionsOperation,
    the "Delete Revision" will be removed from the Revisions Bin, causing the associated revisions to become orphaned.
    However, you will still be able to access these orphaned revisions from the All Revisions view.

Syntax

Available overloads:
====================
public DeleteRevisionsOperation(string documentId,
bool removeForceCreatedRevisions = false);

public DeleteRevisionsOperation(string documentId,
DateTime? from, DateTime? to, bool removeForceCreatedRevisions = false);

public DeleteRevisionsOperation(List<string> documentIds,
bool removeForceCreatedRevisions = false);

public DeleteRevisionsOperation(List<string> documentIds,
DateTime? from, DateTime? to, bool removeForceCreatedRevisions = false);

public DeleteRevisionsOperation(string documentId,
List<string> revisionsChangeVectors, bool removeForceCreatedRevisions = false);
ParameterTypeDescription
documentIdstringThe ID of the document whose revisions you want to delete.
documentIdsList<string>A list of document IDs whose revisions you want to delete.
removeForceCreatedRevisionsbooltrue - Include force-created revisions in the deletion.
false - Exclude force-created revisions.
fromDateTimeThe start of the date range for the revisions to delete (inclusive).
toDateTimeThe end of the date range for the revisions to delete (inclusive).
revisionsChangeVectorsList<string>A list of change vectors corresponding to the revisions that you want to delete.