Mark Needham

Thoughts on Software Development

MarkLogic: Deleting all the documents in a database

with 4 comments

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:


$uri as xs:string
) as empty-sequence()


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!

Be Sociable, Share!

Written by Mark Needham

June 18th, 2011 at 4:08 pm

Posted in Mark Logic

Tagged with

  • Alex Bleasdale

    Hi Mark,

    I put together this a couple of days ago – useful if you want to clear all data from a forest:

  • Stephan Dowding

    Hi Mark,

    Thanks for this… prevented a visit to “room 7”. ;P

  • Stephan Dowding

    You could of course have done:

    Which deletes the root directory and hence also everything else!

  • @21cfd824303d57022745c51ef6052963:disqus I bow to your superior Mark Logic knowledge 😛