0
votes

I need to delete a bunch of documents programmatically. I can use the javascript "patch" feature in the UI to find these documents, and to add a property to identify them, like so:

if (__document_id.indexOf("baddata") > 0) {
   this.'MakeThisGoAway' = true;
}

But I can't find a way to delete documents in a "patch". (By design I assume)

From Raven docs it looks like the right way to do this is a DELETE request via curl, as described here: https://ravendb.net/docs/article-page/3.5/http/client-api/commands/documents/how-to/delete-or-update-documents-using-index with a request like:

curl -X DELETE "http://localhost:8080/databases/NorthWind/bulk_docs/Raven/DocumentsByEntityName?&query=Tag%3AShops&allowStale=false" 

But the only examples are for deleting all documents in a collection. It looks like I should be able to include a query like "propertyname=MakeThisGoAway&value=true" but I can't find anything in the docs on how specifically that works. Am I missing something?

1

1 Answers

1
votes

I figured this out, I was missing a few steps and also hazy on the syntax for lucene queries.

In case it helps anyone, here's the full set of steps I had to go through to delete Ravendb documents in bulk:

  1. If you need to remove historical documents, exclude the Collection from versioning. If you don't do this, Raven will not let you delete documents with "Raven-Document-Revision-Status": "Historical". Go to System Documents, find the Raven/Versioning/CollectionYouWant, and set "Exclude" to true.

  2. Go to Documents/Patch, choose Collection, choose the Collection you want, and then write a patch script to add a flag field to the data you want to delete. (You can skip this step if you want to delete all documents that have a certain value for a certain field) Also, if this data is read only, you'll need to set that to false. Sample patch script:

    // wrap with a conditional to only affect the data that meets your criteria
    if (this.baddata == 1 || __document_id.indexOf("baddata") > 0) {
        // remove Raven-Read-Only, or the delete will fail
        this['@metadata']['Raven-Read-Only'] = false;  
        // add a flag field to identify the documents you will be deleting
        this.MakeThisGoAway = true;
    }
    
  3. Add the field you just created to the index. Go to the index, hit the pencil button, and add "MakeThisGoAway = doc.MakeThisGoAway" to the select statement in its map. Then save, and force a reindex by reopening the index in studio and doing "Index Entries" under the Gear icon.

  4. Delete all documents with the flag field you added, by using curl or postman to send a DELETE to:

    http://ravenserver:8080/databases/DbName/bulk_docs/This/Is/Your/Index?query="MakeThisGoAway:true"
    
  5. Check to be sure the documents you wanted gone are gone. If all is well, tidy up by removing the flag property from the index, and then setting the Versioning document back to Exclude:false.