Delete
The delete command is used to delete objects from the database.
1. delete Hero
2. filter .name = 'Iron Man';
Clauses
Deletion statements support filter, order by, and offset/limit clauses. See EdgeQL > Select for full documentation on these clauses.
1. delete Hero
2. filter .name ilike 'the %'
3. order by .name
4. offset 10
5. limit 5;
Link deletion
Every link is associated with a link deletion policy. By default, it isn’t possible to delete an object linked to by another.
1. db>
1. delete Hero filter .name = "Yelena Belova";
1. ConstraintViolationError: deletion of default::Hero
2. (af7076e0-3e98-11ec-abb3-b3435bbe7c7e) is prohibited by link target policy
3. {}
This deletion failed because Yelena is still in the characters list of the Black Widow movie. We must destroy this link before Yelena can be deleted.
1. db>
2. ...
3. ...
4. ...
5. ...
1. update Movie
2. filter .title = "Black Widow"
3. set {
4. characters -= (select Hero filter .name = "Yelena Belova")
5. };
1. {default::Movie {id: af706c7c-3e98-11ec-abb3-4bbf3f18a61a}}
1. db>
1. delete Hero filter .name = "Yelena Belova";
1. {default::Hero {id: af7076e0-3e98-11ec-abb3-b3435bbe7c7e}}
To avoid this behavior, we could update the Movie.characters link to use the allow deletion policy.
1. type Movie {
2. required property title -> str { constraint exclusive };
3. required property release_year -> int64;
4. multi link characters -> Person;
5. multi link characters -> Person {
6. on target delete allow;
7. };
8. }
Cascading deletes
If a link uses the delete source policy, then deleting a target of the link will also delete the object that links to it (the source). This behavior can be used to implement cascading deletes; be careful with this power!
The full list of deletion policies is documented at Schema > Links.
Return value
A delete statement returns the set of deleted objects. You can pass this set into select to fetch properties and links of the (now-deleted) objects. This is the last moment this data will be available before being permanently deleted.
1. db>
2. ...
1. with movie := (delete Movie filter .title = "Untitled")
2. select movie {id, title};
1. {default::Movie {
2. id: b11303c6-40ac-11ec-a77d-d393cdedde83,
3. title: 'Untitled',
4. }}
︙
| See also |
| Reference > Commands > Delete |
| Cheatsheets > Deleting data |
