MarkLogic is a powerful database management system that allows you to store, retrieve, and analyze large amounts of data. However, there may come a time when you need to clear the database and start fresh. Whether you want to remove all the data or just specific documents, this comprehensive guide will walk you through the process step by step.
Before You Begin
Before you clear the database, it's important to understand the implications of this action. Clearing the database will permanently delete all the data it contains. This means that any documents, collections, or indexes will be lost. Make sure to back up any important data before proceeding.
Clearing the Entire Database
If you want to remove all the data from the database, you can use the following steps:
- Open the MarkLogic Query Console.
- Log in to your MarkLogic account using your credentials.
- Run the following XQuery code to delete all the documents in the database:
xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin"
at "/MarkLogic/admin.xqy";
let $config := admin:get-configuration()
let $dbid := xdmp:database("your-database-name")
let $forest-ids := admin:database-forests($config, $dbid)
for $forest-id in $forest-ids
let $forest-name := admin:forest-name($config, $forest-id)
return
(
xdmp:eval(
'xdmp:document-delete(fn:doc(), xdmp:forest("' || $forest-name || '"))',
(),
{xdmp:database("your-database-name")}
)
)
Make sure to replace "your-database-name" with the actual name of your database. This code will delete all the documents in the specified database.
Clearing Specific Documents
If you only want to remove specific documents from the database, you can use the following steps:
- Open the MarkLogic Query Console.
- Log in to your MarkLogic account using your credentials.
- Run the following XQuery code to delete specific documents:
xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin"
at "/MarkLogic/admin.xqy";
let $config := admin:get-configuration()
let $dbid := xdmp:database("your-database-name")
let $forest-ids := admin:database-forests($config, $dbid)
let $uris := ("document1.xml", "document2.xml", "document3.xml")
for $forest-id in $forest-ids
let $forest-name := admin:forest-name($config, $forest-id)
return
(
xdmp:eval(
'xdmp:document-delete((' || string-join($uris, ',') || '), xdmp:forest("' || $forest-name || '"))',
(),
{xdmp:database("your-database-name")}
)
)
Replace "your-database-name" with the actual name of your database. Also, modify the $uris variable to include the URIs of the documents you want to delete. This code will delete the specified documents from the database.
Clearing the database in MarkLogic is a straightforward process, but it's essential to understand the consequences. Always back up your data before performing any deletion operations. Whether you want to remove all the data or just specific documents, the provided XQuery code will help you achieve your goal.
| Reference | Link |
|---|---|
| MarkLogic Documentation | https://docs.marklogic.com/ |
| MarkLogic Query Console | https://docs.marklogic.com/guide/qconsole |