MarkLogic: Deleting all the documents in a database
We’re using the MarkLogic database on my current project and something that we wanted to do recently was delete all the documents as part of a deployment script.
Getting all of the documents is reasonably easy - we just need to make a call to the doc() function.
We can then iterate through the documents like so:
for $doc in doc() return $doc
We wanted to make use of the xdmp:document-delete function to tear down all of the modules but that needs a uri representing the location of the document in the database which isn’t available in $doc:
xdmp:document-deletexdmp:document-delete( $uri as xs:string ) as empty-sequence() Summary: Deletes a document from the database.
A colleague pointed out that what we needed to do was pass the document to xdmp:node-uri and then our troubles would be over!
The final solution therefore looks like this:
for $doc in doc() return xdmp:document-delete(xdmp:node-uri($doc))
I was expecting that we would delete the documents by some sort of identifier but I guess this approach makes more sense given the way the data is stored.
It all seems a bit esoteric at the moment!