1

How to add a loop to list all the forests for a given database and detach all forests accordingly?

xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin"
  at "/MarkLogic/admin.xqy";
    
let $config := admin:get-configuration()
return
admin:
database-detach-forest($config, xdmp:database("myDatabase"),
  xdmp:forest("myForest") )
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
CJ Chang
  • 325
  • 4
  • 12

1 Answers1

3

Each time you call admin:database-detach-forest() it will return a modified configuration. You will want to use that updated config in subsequent calls.

You could invoke a tail recursive function that removes each forest and uses the updated config to make the call to remove the next, and then finally returns the last updated configuration:

xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin"
          at "/MarkLogic/admin.xqy";

declare function local:detach-forests($config, $database as xs:unsignedLong, $forests as xs:unsignedLong*) {

  let $forest := head($forests)
  return
    if ($forest)
    then (
      let $newConfig := admin:database-detach-forest($config, $database, $forest)
      return local:detach-forests($newConfig, $database, tail($forests))
    )
    else $config
};

let $config := admin:get-configuration()
let $database := xdmp:database("myDatabase")
let $forests := xdmp:database-forests($database)

let $newConfig := local:detach-forests($config, $database, $forests)
return 
  admin:save-configuration($newConfig)

or you can cheat and use xdmp:set() to mutate the $config variable.

xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin"
          at "/MarkLogic/admin.xqy";
let $config := admin:get-configuration()
let $database := xdmp:database("myDatabase")

let $_ :=
  for $forest in xdmp:database-forests($database)
  return xdmp:set($config, admin:database-detach-forest($config, $database, $forest) )
  
return 
  admin:save-configuration($config)

Whichever way you choose, then save that new configuration with admin:save-configuration() to persist the change.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147