marklogic Very simple CRUD examples for XML documents Delete - Last, but not least


Example

To round out simple examples of CRUD operations, we present the following examples. Always use great care in deleting documents.

(: When we know the URI, we can delete it very easily :)
let $uri := "/stuff/mysimpledocument.xml"
return xdmp:document-delete($uri)

or simplified:

xdmp:document-delete("/stuff/mysimpledocument.xml")

You can certainly use XPath to find the document, get the URI of it, and then delete it with something like this, but the danger is that any documents that are returned by the XPath expressions will be removed. Not always a good thing.

(: Use caution when using XPath to select target docs to delete :)
for $doc in /my-document
return xdmp:document-delete(fn:base-uri($doc))

Want to delete all documents? This will do it, but be very careful you know what database your code will execute against.

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