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);
// 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.SendAsync
var numberOfRevisionsDeleted = await store.Maintenance.SendAsync(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 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 = await store.Maintenance.SendAsync(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);
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 = await store.Maintenance.SendAsync(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);
// Get the change-vectors for the revisions of the specified document
var metadata = await asyncSession.Advanced.Revisions
    .GetMetadataForAsync("orders/825-A");
    
var revisionsChangeVectors = metadata
    .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 = await store.Maintenance.SendAsync(deleteRevisionsOp);

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);
Parameter Type Description
documentId string The ID of the document whose revisions you want to delete.
documentIds List<string> A list of document IDs whose revisions you want to delete.
removeForceCreatedRevisions bool true - Include force-created revisions in the deletion.
false - Exclude force-created revisions.
from DateTime The start of the date range for the revisions to delete (inclusive).
to DateTime The end of the date range for the revisions to delete (inclusive).
revisionsChangeVectors List<string> A list of change vectors corresponding to the revisions that you want to delete.