Delete

Delete( ref )
Delete( ref )
Delete( ref )
Delete( ref )
delete( ref )
Delete( ref )

Description

The Delete function removes a document. This includes user-created documents, plus system documents for Collections, Indexes, Databases, etc.

When you delete a Databases, all of its documents, Collections, child Databases, Functions, Indexes, Keys, Roles, and Tokens are also deleted.

When you delete a Collection, all documents within the Collection are also deleted.

When you delete a Collection that is the single source for an index, the index is also deleted.

There is no cascading delete for user-defined documents. If you have a document "A" that includes a reference to another document "B", deleting "A" does not affect "B", and deleting "B" does not affect "A". You would have to write your own query logic to automatically follow references to perform a cascading delete.

For performance, Databases, Collections, Functions, Indexes, Keys, and Roles use an object cache. When you use Delete to delete any of these FaunaDB schema documents, the deletion is not guaranteed to be visible immediately.

Parameters

Argument Type Definition and Requirements

ref

Reference

The reference to an object that was removed.

Returns

An object containing the metadata about the delete operations.

Field Name Field Type Definition and Requirements

database

String

The logical name of an existing database.

role

String

The access roles include admin, server, server-readonly, or client.

data

Object

Optional - This is user-defined metadata for the key. It is provided for the developer to store information at the key level.

priority

Long

Optional - A relative weight between 1 and 500, inclusive, indicating how many resources this database is allowed to utilize. Defaults to 1. A higher number means more resources.

The priority option is deprecated as of release 2.10.0. You should avoid specifying priority. In some future FaunaDB release, priority will be removed. See Deprecations for more details.

Examples

The query below removes the document pointed at by the reference.

client.Query(Delete(Ref(Collection("spells"), "181388642581742080")));
{
  "ref": { "@ref": "classes/spells/181388642581742080" },
  "class": { "@ref": "classes/spells" },
  "ts": 1509244539764856,
  "data": {
    "name": "Mountain's Thunder",
    "element": [ "air", "earth" ],
    "cost": 10
  }
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{ "delete": { "@ref": "classes/spells/181388642581742080" } }'
HTTP/1.1 200 OK
{
  "resource": {
    "ref": { "@ref": "classes/spells/181388642581742080" },
    "class": { "@ref": "classes/spells" },
    "ts": 1509244539764856,
    "data": {
      "name": "Mountain's Thunder",
      "element": [ "air", "earth" ],
      "cost": 10
    }
  }
}
result, _ := client.Query(
    f.Delete(f.RefCollection(f.Collection("spells"), "181388642581742080")),
)

fmt.Println(result)
map[ref:{181388642581742080 0xc4201f3da0 <nil>} ts:1509244539764856 data:map[cost:10 element:[air earth] name:Mountain's Thunder]]
System.out.println(
        client.query(
                Delete(Ref(Collection("spells"), Value(181388642581742080L)))
        ).get());
{
  ref: ref(id = "181388642581742080", collection = ref(id = "spells", collection = ref(id = "collections"))),
  ts: 1509244539764856,
  data: {
    name: "Mountain's Thunder",
    element: ["air", "earth"],
    cost: 10
  }
}
client.query(q.Delete(q.Ref(q.Collection('spells'), '181388642581742080')))
.then((ret) => console.log(ret))
{ ref:
   Ref(id=181388642581742080, collection=Ref(id=spells, collection=Ref(id=collections))),
  ts: 1527275280180078,
  data:
   { name: 'Mountain\'s Thunder',
     element: [ 'air', 'earth' ],
     cost: 10 } }
client.query(q.delete(q.ref(q.collection("spells"), "181388642581742080")))
{
  "ref": { "@ref": "classes/spells/181388642581742080" },
  "class": { "@ref": "classes/spells" },
  "ts": 1509244539764856,
  "data": {
    "name": "Mountain's Thunder",
    "element": [ "air", "earth" ],
    "cost": 10
  }
}
client.query(Delete(Ref(Collection("spells"), "181388642581742080")))
{
  "ref": { "@ref": "classes/spells/181388642581742080" },
  "class": { "@ref": "classes/spells" },
  "ts": 1509244539764856,
  "data": {
    "name": "Mountain's Thunder",
    "element": [ "air", "earth" ],
    "cost": 10
  }
}

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!