Delete database entities
This tutorial deletes in order:
-
A document field.
-
A document in the collection.
-
The collection.
Create database entities for this tutorial
To preserve the CoffeeBean collection for the following tutorials, better
show the scope of delete operations, and provide a self-contained example,
this tutorial first creates and populates another arbitrary collection.
Create a temporary collection
Similar to how you created a collection in the Populate the cookbook database tutorial, create a collection.
-
In the Dashboard Explorer page, under your Region Group, choose the
Cookbookdatabase. -
In the left-bottom panel, choose Collections and click the plus (
+) symbol to the right of theCookbookdatabase. -
Create a collection of information about stores. In the Create Collection dialog, enter
Storefor the collection Name. -
Leave all other fields in their default state and click the CREATE button.
You should see the
Storecollection added to your database, and if you select theStorecollection, you should see an empty collection in the Shell:
collection Store {
history_days 0
}
Add documents to the temporary collection
-
In the Shell for the
Cookbookdatabase, add three documents with the following queries: -
Confirm that the
Storecollection is now populated with three documents by selecting theStorecollection in the Dashboard and viewing the Documents list or by running the following query:{ data: [ { id: "378661942830563393", coll: Store, ts: Time("2023-10-15T14:10:19.500Z"), name: "DC Fruits", address: { street: "13 Pierstorff Drive", city: "Washington", state: "DC", zipCode: "20220" } }, { id: "378662034803261505", coll: Store, ts: Time("2023-10-15T14:11:47.230Z"), name: "Party Supplies", address: { street: "7529 Capitalsaurus Court", city: "Washington", state: "DC", zipCode: "20002" } }, { id: "378662129421516865", coll: Store, ts: Time("2023-10-15T14:13:17.455Z"), name: "Foggy Bottom Market", address: { street: "4 Florida Ave", city: "Washington", state: "DC", zipCode: "20037" } } ] }
The rest of this tutorial shows how to delete database elements and finishes
with deleting the Store collection.
Delete a field
Delete a field in a document by setting its value to null.
Using the document id of one of the documents in your Store collection to
get the document, use the following query to delete the address field:
{
id: "378661942830563393",
coll: Store,
ts: Time("2023-10-15T14:33:05.890Z"),
name: "DC Fruits"
}
The response shows that the address field is deleted, and you can run
Store.all() to view the updated store data.
Remember that deleted fields aren’t saved by Fauna.
Delete a document
To delete the full document and remove it from the collection, call the
delete() method on the
document instance. Again, use a document id from your Store collection:
Store.byId("378662129421516865") /* not found */
The not found response confirms that the document is deleted.
Run Store.all() to see that there are now two documents in the collection.
If you delete a document referenced by another document, the referencing
document field is replaced with a Deleted placeholder that shows that the
document is deleted but the reference still exists. Try this example:
-
Create two collections:
-
Create a document in both collections such that one document references the document in the other collection:
{ id: "380934695755448388", coll: Foo, ts: Time("2023-11-09T16:14:45.570Z"), other: Bar.byId("380934695754399812") } -
Delete the referenced document:
-
View the document definition of the referencing document:
{ id: "380934695755448388", coll: Foo, ts: Time("2023-11-09T16:14:45.570Z"), other: Deleted.byId("380934695754399812") }The document referenced as other is deleted but the reference still exists.
Delete a collection
You delete a collection by deleting the collection
definition,
which describes the schema for the collection. To view the Store collection
definition, run Store.definition.
To delete the collection, run the following query:
Collection.byName("Store") /* not found */
The not found response confirms that the collection is deleted.
Deleting a collection deletes its indexes, user-defined functions, and all documents in the collection.
Run Collection.all() to list the current collections in the system,
confirming that the Store collection no longer exists.
Delete other database entities
See the Use indexes for more efficient queries tutorial for how to delete an index and see the Write custom logic with UDFs tutorial for how to delete a user-defined function.
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!