Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all documents matching a query?

I would like to delete all documents matching some predicates. The query I have come up with is as follows, but nothing is deleted from the database.

I suspect this is because the $doc is set to the XML value of the document rather than the document itself. Can anyone shed any light on this?

xquery version "1.0-ml";
for $doc in cts:search(fn:collection("MYCOLLECTIONNAME")/MyDocumentRoot,
    cts:or-query((
    cts:element-range-query (xs:QName("MyElement"), "=", "MyElementValue"),
    )), "unfiltered" )
    return xdmp:document-delete($doc);

The document looks like

<MyDocumentRoot>
  <MyElementName>MyElementValue</MyElementName>
</MyDocumentRoot>
like image 578
Ben Aston Avatar asked Sep 10 '26 07:09

Ben Aston


2 Answers

You are indeed passing the contents of the documents into xdmp:document-delete instead of its uri. You could derive the uri using for instance fn:base-uri(), but like this all docs you want to delete are retrieved from the database first, which is unnecessary.

Instead, enable the URI lexicon, and use cts:uris to do the deletion. It might also be wise to do the deletion in batches of lets say 1000 docs.

HTH!

like image 120
grtjn Avatar answered Sep 13 '26 07:09

grtjn


xdmp:document-delete() takes the URI of the document, rather than a node. So the simplest fix would be to wrap $doc in base-uri(.):

return xdmp:document-delete(base-uri($doc))

But if you have the URI lexicon enabled, you can write a much faster query like this:

let $query := cts:and-query((
                cts:collection-query("MYCOLLECTIONNAME"),
                cts:or-query((...)) (: put your full or query here :)
              ))
for $uri in cts:uris("",(),$query) return xdmp:document-delete($uri)

In the latter case, you avoid having to read each document to get its URI.

like image 41
Evan Lenz Avatar answered Sep 13 '26 05:09

Evan Lenz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!