How to delete and remove objects
You can use the methods delete()
and remove()
to construct queries that will
remove nodes and relationships or remove properties and labels.
delete(variable_expressions: Union[str, List[str]], detach: Optional[bool] = False)
- Remove nodes and relationships. Set the argumentdetach
to true in order to delete nodes with their relationships.remove(items: Union[str, List[str]])
- Remove properties and labels.
Delete nodes
To delete a node from the database, use the delete()
method:
- GQLAlchemy
- Cypher
from gqlalchemy import Match
query = Match()
.node(labels="Person", variable="p")
.delete(variable_expressions="p")
.execute()
MATCH (p:Person) DELETE p;
Delete relationships
To delete a relationship from the database, use the delete()
method:
- GQLAlchemy
- Cypher
from gqlalchemy import match
query = Match()
.node(labels="Person")
.to(relationship_type="FRIENDS_WITH", variable="f")
.node(labels="Person")
.delete(variable_expressions="f")
.execute()
MATCH (:Person)-[f:FRIENDS_WITH]->(:Person) DELETE f;
Remove properties
To remove a property (or properties) from the database, use the remove()
method:
- GQLAlchemy
- Cypher
from gqlalchemy import Match
query = Match()
.node(labels="Person", variable="p")
.remove(items=["p.name", "p.last_name"])
MATCH (p:Person) REMOVE p.name;