Delete database entities

This tutorial shows you how to delete a:

  • Document field

  • Document

  • Collection

The tutorial’s examples use Fauna’s demo data. Several examples use byId() to fetch a specific document. To run these examples, create a Product document with the following id and document fields:

// Create a document
Product.create({
  id: "392886847463751746",
  name: "widget",
  description: "Standard widget"
})

Delete a field

To delete a document field, set its value to null.

The following query uses update() to set the description field to null:

// Delete the `description` field
Product.byId("392886847463751746")!.update({
  description: null
})

The returned document no longer includes the field:

{
  id: "392886847463751746",
  coll: Product,
  ts: Time("2024-03-28T18:18:41.170Z"),
  name: "widget",
  price: 24.99
}

Delete a document

Use delete() to delete a document. Deleting a document removes it from its collection.

// Delete a document
Product.byId("392886847463751746")!.delete()

If successful, the query returns a NullDoc that confirms the document was deleted.

Product.byId("392886847463751746") /* deleted */

Documents that reference the deleted document return the NullDoc in projections.

Delete a collection

You can delete a collection using the Fauna Dashboard or Fauna CLI. With the CLI:

  1. Delete the .fsl for the collection schema.

  2. Use the fauna schema push command to push the updated schema to Fauna.

Use Collection.all() to verify the collection no longer exists. The following query returns a list of collection names in the database.

// Get all collection names
Collection.all() { name }

Deleting a collection deletes its documents, indexes, and user-defined functions.

Delete other database entities

To delete an index, see Use indexes for more efficient queries. To delete a user-defined function, see Write custom logic with UDFs.

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!