Delete a collection

Problem

You need to delete a collection, including all of its documents, from the current database.

Solution

Use the Delete function:

try
{
    Value result = await client.Query(
        Delete(Collection("dilapidated_huts"))
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(ref: RefV(id = "dilapidated_huts", collection = RefV(id = "collections")),ts: LongV(1622573797800000),history_days: LongV(30),name: StringV(dilapidated_huts))
result, err := client.Query(
	f.Delete(f.Collection("dilapidated_huts")))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[history_days:30 name:dilapidated_huts ref:{dilapidated_huts 0xc00007a4b0 0xc00007a4b0 <nil>} ts:1622573818060000]
System.out.println(
    client.query(
        Delete(Collection("dilapidated_huts"))
    ).get());
{ref: ref(id = "dilapidated_huts", collection = ref(id = "collections")), ts: 1622573868770000, history_days: 30, name: "dilapidated_huts"}
client.query(
  q.Delete(q.Collection('dilapidated_huts'))
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
{
  ref: Collection("dilapidated_huts"),
  ts: 1622573895370000,
  history_days: 30,
  name: 'dilapidated_huts'
}
result = client.query(
  q.delete(q.collection("dilapidated_huts"))
)
print(result)
{'ref': Ref(id=dilapidated_huts, collection=Ref(id=collections)), 'ts': 1622573900340000, 'history_days': 30, 'name': 'dilapidated_huts'}
try {
  println(Await.result(
    client.query(
        Delete(Collection("dilapidated_huts"))
    ),
    5.seconds
  ))
} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
{ref: ref(id = "dilapidated_huts", collection = ref(id = "collections")), ts: 1622574067600000, history_days: 30, name: "dilapidated_huts"}
Delete(Collection('dilapidated_huts'))
{
  ref: Collection("dilapidated_huts"),
  ts: 1624310573640000,
  history_days: 30,
  name: 'dilapidated_huts'
}

Discussion

When a collection is deleted:

  • All of the documents contained in the collection are also deleted.

  • Any indexes with a single source matching the collection are also deleted.

  • Any indexes that have multiple source collections are not deleted.

  • References to the collection, or to documents within the collection, that are stored in any documents outside of the collection, are not modified and are no longer valid.

More information

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!