Hi Nicholas,
If I run replace.xq, shown below, it fails with Out of Main Memory
as you already experienced in your tests, the replace command takes less memory than the db:replace() function, as the second one will first cache all update operations on the Pending Update List [1] before actually executing them.
The only idea I have is to use xquery to generate the file replace.bxs and then run it.
This is indeed a popular approach. If all of your files are stored in a single directory, you could as well try to delete your documents and add all files via a single db:add call:
db:delete("database"), db:add("database", "/path/to/dir")
Hope this helps, Christian
[1] http://docs.basex.org/wiki/XQuery_Update#Pending_Update_List