Delete all Nodes and Relationships in a Neo4j Database

Deleting Nodes and Relationships

Deleting all nodes and relationships in a Neo4j database is very simple. Here is an example that does just that:


The DETACH keyword specifies to remove or “detach” all relationships from a particular node before deletion. If relationships exist on a node at the time deletion is attempted, an exception will be thrown and you might see a message like “To delete this node, you must first delete its relationships“.

Deleting Nodes only

If no relationships exist on the nodes in your database, you can simply remove “DETACH” from the query above. For example:


Unique Constraint Exceptions after Deleting Nodes

If unique constraints exist on nodes, you may see exceptions when trying to recreate nodes that previously existed. Dropping unique constraints before or after deleting nodes can help you avoid these issues.

Here is example of dropping a unique constraint on the :User label and the username property:

DROP CONSTRAINT ON (user:User) ASSERT user.username IS UNIQUE;

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">