How to delete documents using WOQL
To use this HowTo, first clone the Star Wars demo into your team on the DFRNT TerminusDB cloud. You will then have access to the data needed for this tutorial.
Delete a document in WOQL
Deleting a document in WOQL is possible using the delete_document
keyword.
First, let's insert a document.
let v = Vars("id");
insert_document(doc({'@type' : 'Planet', label: 'Planet-X'}), v.id)
Supposing we get back the following:
"Planet/01dd97a75800f01f43ab7ab55b6dd08f198dd34d2bdbbeeb7bf4edee45111863"
Now we can delete it with the following:
delete_document("Planet/01dd97a75800f01f43ab7ab55b6dd08f198dd34d2bdbbeeb7bf4edee45111863")
Delete a subdocument in WOQL
Subdocuments can be deleted using the delete_document
keyword, but it's important to also delete the triple that links the subdocument from the parent document. Here we resolve the parent document in the variable v:parentdoc
.
and(
eq("v:subdoc", "Person/John/role/PersonRole/cxW1Egirxm8-QYrq"),
triple("v:parentdoc", "role", "v:subdoc"),
delete_document("v:subdoc"),
delete_triple("v:parentdoc", "role", "v:subdoc"),
)