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 http://docs.marklogic.com/4.2doc/docapp.xqy#search.xqy?start=1&cat=all&query=xdmp:document-delete&button=search 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 http://docs.marklogic.com/4.2doc/docapp.xqy#search.xqy?start=1&cat=all&query=xdmp:node-uri&button=search 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!
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.