DELETE - Neo4j Cypher Manual

Skip to content Cypher Manual Product Version Version 25 Version 5 Version 4.4
    • Introduction
      • Overview
      • Cypher and Neo4j
      • Cypher and Aura
    • Queries
      • Core concepts
      • Basic queries
      • Select Cypher version
      • Composed queries
        • Combined queries (UNION)
        • Conditional queries (WHEN)
        • Sequential queries (NEXT)
    • Clauses
      • Clause composition
      • CALL procedure
      • CREATE
      • DELETE
      • FILTER
      • FINISH
      • FOREACH
      • LET
      • LIMIT
      • LOAD CSV
      • MATCH
      • MERGE
      • OPTIONAL MATCH
      • ORDER BY
      • REMOVE
      • RETURN
      • SET
      • SHOW FUNCTIONS
      • SHOW PROCEDURES
      • SHOW SETTINGS
      • SHOW TRANSACTIONS
      • SKIP
      • TERMINATE TRANSACTIONS
      • UNWIND
      • USE
      • WHERE
      • WITH
    • Subqueries
      • CALL subqueries
      • CALL subqueries in transactions
      • COLLECT subqueries
      • COUNT subqueries
      • EXISTS subqueries
    • Patterns
      • Primer
      • Fixed-length patterns
      • Variable-length patterns
      • Shortest paths
      • Non-linear patterns
      • Match modes
      • Syntax and semantics
    • Values and types
      • Property, structural, and constructed values
      • Boolean, numeric, and string literals
      • Temporal values
      • Spatial values
      • Lists
      • Maps
      • Vectors
      • Graph references
      • Working with null
      • Casting data values
      • Equality, ordering, and comparison of value types
    • Expressions
      • Predicates
        • Boolean operators
        • Comparison operators
        • List operators
        • String operators
        • Path pattern expressions
        • Type predicate expressions
      • Node and relationship operators
      • Mathematical operators
      • String concatenation operators
      • Temporal operators
      • List expressions
      • Map expressions
      • Conditional expressions (CASE)
    • Functions
      • Aggregating functions
      • Database functions
      • Graph functions
      • List functions
      • LOAD CSV functions
      • Mathematical functions
        • Logarithmic functions
        • Numeric functions
        • Trigonometric functions
      • Predicate functions
      • Scalar functions
      • Spatial functions
      • String functions
      • Temporal functions
        • Duration functions
        • Instant type functions
        • Format functions
      • User-defined functions
      • Vector functions
    • Indexes
      • Search-performance indexes
        • Create, show, and drop indexes
        • The impact of indexes on query performance
        • Index hints for the Cypher planner
      • Semantic indexes
        • Full-text indexes
        • Vector indexes
      • Syntax
    • Constraints
      • Create, show, and drop constraints
      • Syntax
    • Execution plans and query tuning
      • Understanding execution plans
      • Operators
        • Operators in detail
      • Cypher runtimes
        • Concepts
        • Parallel runtime: reference
      • Query tuning
    • Query caches
      • Unifying query caches
    • Administration
    • Syntax
      • Parsing
      • Naming rules and recommendations
      • Variables
      • Keywords
      • Parameters
      • Comments
    • Additions, deprecations, removals, and compatibility
    • Appendix
      • Cypher styleguide
      • GQL conformance
        • Supported mandatory GQL features
        • Currently unsupported mandatory GQL features
        • Supported optional GQL features
        • Optional GQL features and analogous Cypher
        • Additional Cypher features
      • Tutorials and extended examples
        • Basic query tuning example
        • Advanced query tuning example

Is this page helpful?

  • Cypher Manual
  • Clauses
  • DELETE
Raise an issue DELETE

The DELETE clause is used to delete nodes, relationships or paths.

For removing properties and labels, see the REMOVE clause.

It is not possible to delete nodes with relationships connected to them without also deleting the relationships. This can be done by either explicitly deleting specific relationships, or by using the DETACH DELETE clause.

While the DELETE clause renders the deleted objects no longer accessible, the space occupied by the deleted nodes and relationships remain on the disk and is reserved for future transactions creating data. For information about how to clear and reuse the space occupied by deleted objects, see Operations Manual → Space reuse.

Example graph

The following graph is used for the examples below. It shows four actors, three of whom ACTED_IN the Movie The Matrix (Keanu Reeves, Carrie-Anne Moss, and Laurence Fishburne), and one actor who did not act in it (Tom Hanks).

Example graph connecting person nodes to a movie node and an extra person node not connected to the movie node via an acted in relationship

To recreate the graph, run the following query in an empty Neo4j database:

CREATE (keanu:Person {name: 'Keanu Reever'}), (laurence:Person {name: 'Laurence Fishburne'}), (carrie:Person {name: 'Carrie-Anne Moss'}), (tom:Person {name: 'Tom Hanks'}), (theMatrix:Movie {title: 'The Matrix'}), (keanu)-[:ACTED_IN]->(theMatrix), (laurence)-[:ACTED_IN]->(theMatrix), (carrie)-[:ACTED_IN]->(theMatrix)

Delete single node

To delete a single node, use the DELETE clause:

Query MATCH (n:Person {name: 'Tom Hanks'}) DELETE n

This deletes the Person node Tom Hanks. This query is only possible to run on nodes without any relationships connected to them.

Result Deleted 1 node

NODETACH keyword

It is also possible to delete the single node using the NODETACH DELETE clause. Using the NODETACH keyword explicitly defines that relationships will not be detached and deleted from a node. The NODETACH keyword is a mirror of the already existing keyword DETACH, and it was introduced as part of Cypher®'s GQL conformance. Including it is functionally the same as using simple DELETE.

Query MATCH (n:Person {name: 'Tom Hanks'}) NODETACH DELETE n

This also deletes the Person node Tom Hanks.

Delete relationships only

It is possible to delete a relationship while leaving the node(s) connected to that relationship otherwise unaffected.

Query MATCH (n:Person {name: 'Laurence Fishburne'})-[r:ACTED_IN]->() DELETE r

This deletes all outgoing ACTED_IN relationships from the Person node Laurence Fishburne, without deleting the node.

Result Deleted 1 relationship

Delete a node with all its relationships

To delete nodes and any relationships connected them, use the DETACH DELETE clause.

Query MATCH (n:Person {name: 'Carrie-Anne Moss'}) DETACH DELETE n

This deletes the Person node Carrie-Anne Moss and all relationships connected to it.

Result Deleted 1 node, deleted 1 relationship

The DETACH DELETE clause may not be permitted to users with restricted security privileges. For more information, see Operations Manual → Fine-grained access control.

Delete all nodes and relationships

It is possible to delete all nodes and relationships in a graph.

Delete all nodes and relationships MATCH (n) DETACH DELETE n Result Deleted 3 nodes, deleted 1 relationship

DETACH DELETE is useful when experimenting with small example datasets, but it is not suitable for deleting large amounts of data, nor does it delete indexes and constraints.

To delete large amounts of data without deleting indexes and constraints, use CALL subqueries in transactions instead.

Delete all nodes and relationships using CALL subqueries MATCH (n) CALL (n) { DETACH DELETE n } IN TRANSACTIONS

To remove all data, including indexes and constraints, recreate the database using the following command: CREATE OR REPLACE DATABASE name.

Delete a database and recreate it CREATE OR REPLACE DATABASE neo4j

For more information, see the Operations Manual → Create databases with IF NOT EXISTS or OR REPLACE.

CREATE FILTER

Hands-on Cypher Course

End-to-end Cypher training with Neo4j GraphAcademy

View courses

Từ khóa » Xóa 4