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(1635183024850000),history_days: LongV(30),name: StringV(dilapidated_huts),data: ObjectV(env: StringV(test)))
result, err := client.Query(
	f.Delete(f.Collection("dilapidated_huts")))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[data:map[env:test] history_days:30 name:dilapidated_huts ref:{dilapidated_huts 0xc0001844b0 0xc0001844b0 <nil>} ts:1635183036860000]
System.out.println(
    client.query(
        Delete(Collection("dilapidated_huts"))
    ).get());
{ref: ref(id = "dilapidated_huts", collection = ref(id = "collections")), ts: 1635183075770000, history_days: 30, name: "dilapidated_huts", data: {env: "test"}}
client.query(
  q.Delete(q.Collection('dilapidated_huts'))
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  ref: Collection("dilapidated_huts"),
  ts: 1635183086600000,
  history_days: 30,
  name: 'dilapidated_huts',
  data: { env: 'test' }
}
result = client.query(
  q.delete(q.collection("dilapidated_huts"))
)
print(result)
{'ref': Ref(id=dilapidated_huts, collection=Ref(id=collections)), 'ts': 1635183090850000, 'history_days': 30, 'name': 'dilapidated_huts', 'data': {'env': 'test'}}
Delete(Collection('dilapidated_huts'))
{
  ref: Collection("dilapidated_huts"),
  ts: 1635183253930000,
  history_days: 30,
  name: 'dilapidated_huts',
  data: { env: 'test' }
}
Query metrics:
  •    bytesIn:   44

  •   bytesOut:  186

  • computeOps:    1

  •    readOps:    0

  •   writeOps:    1

  •  readBytes:  255

  • writeBytes:  385

  •  queryTime: 46ms

  •    retries:    0

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.

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!