Re: [Neo4j] REST API - delete all nodes and relationships ( preferably thru Cypher)

2,544 views
Skip to first unread message

Julian Simpson

unread,
Jul 18, 2012, 9:22:37 AM7/18/12
to ne...@googlegroups.com


On Wed, Jul 18, 2012 at 11:49 AM, Aidan Casey <aidan...@gmail.com> wrote:
I'm running Neo4J on Heroku and accessing it through the REST API (node.js client).

I can't figure out how to delete all nodes and all relationships in the database except for node 0.

Basically I want to clear all the data out of my database and repopulate it several times during development - is this possible through a cypher query or through the REST API???

Here's what I've been using (for the exact same reasons as you):

START n=node(*)
MATCH n-[r?]-()
WHERE ID(n) <> 0
DELETE n,r

Best

Julian.
 


thanks

Aidan



--
Julian Simpson 
Software Engineer | Neo Technology
+447967189579 | @builddoctor
As a friend of Neo, register for GraphConnect and get a 20% discount. Code: GCFON

Aidan Casey

unread,
Jul 21, 2012, 5:26:13 AM7/21/12
to ne...@googlegroups.com
Fantastico !!!
thanks for the swift reply!


On Wednesday, July 18, 2012 8:49:36 PM UTC+10, Aidan Casey wrote:
I'm running Neo4J on Heroku and accessing it through the REST API (node.js client).

I can't figure out how to delete all nodes and all relationships in the database except for node 0.

Basically I want to clear all the data out of my database and repopulate it several times during development - is this possible through a cypher query or through the REST API???


thanks

Aidan

Nigel Small

unread,
Jul 21, 2012, 6:48:08 AM7/21/12
to ne...@googlegroups.com
Since a complete clear down of the graph (except ref node) seems to be a very common requirement, I've just added a helper method to py2neo to do this, called "clear":


Will be available in the next release.

Cheers
Nige

S B

unread,
Aug 7, 2012, 4:47:10 AM8/7/12
to ne...@googlegroups.com
Nigel,
I would add a 'force=False' in the prototype of the function just to make sure that people really know what they are doing. If you are just fiddling with the library and trying the functions you might create disaster.

Stefan

Nigel Small

unread,
Aug 7, 2012, 3:54:01 PM8/7/12
to ne...@googlegroups.com
Hi Stefan

I'm afraid I have to disagree on this point. If someone is testing out the library, I would hope that they wouldn't be doing so against a production database. Also, the mechanism that you suggest would also not create any real kind of barrier to accidental damage and would merely introduce unnecessary noise into the function signature.

Nige

Christopher Hydak

unread,
Sep 20, 2012, 2:43:46 PM9/20/12
to ne...@googlegroups.com
That method did not work for me.  I'm accessing neo (1.7.2) through the shell and all it gives me is this error message:
SyntaxException: expected return clause

Does 1.7.2 not support DELETE?

Michael Hunger

unread,
Sep 20, 2012, 3:05:20 PM9/20/12
to ne...@googlegroups.com
Christopher,

if you just need it in a testing environment, perhaps my test-delete-db extension helps you.



Michael

--
 
 

Christopher Hydak

unread,
Sep 20, 2012, 3:22:07 PM9/20/12
to ne...@googlegroups.com
That did not work for me.  I have version 1.7.2 but I tried your 1.6 and 1.8 versions.

Michael Hunger

unread,
Sep 20, 2012, 3:31:59 PM9/20/12
to ne...@googlegroups.com
Uploaded one for 1.7

Michael

Christopher Hydak

unread,
Sep 20, 2012, 4:22:16 PM9/20/12
to ne...@googlegroups.com
Thanks.  It's still not working though.  I've double-checked each line but still no dice.

Christopher Hydak

unread,
Sep 20, 2012, 4:24:55 PM9/20/12
to ne...@googlegroups.com
Here is the command and the output I'm getting:









{
  "node-indexes" : [ ],
  "nodes" : 0,
  "relationship-indexes" : [ ],
  "relationships" : 0,
  "maxNodesToDelete" : 1000

Michael Hunger

unread,
Sep 20, 2012, 4:38:56 PM9/20/12
to ne...@googlegroups.com
The display in the webadmin-monitoring is actually the "highest-id in use". Not the actual number of nodes & rels.

You can check with a cypher read query that they are gone.

Michael

--
 
 

Christopher Hydak

unread,
Sep 20, 2012, 4:56:32 PM9/20/12
to ne...@googlegroups.com
I think your code works, but there was something peculiar about my data that I was trying to delete.  So, I completely deleted my database and had neo4j recreate it.  Now, when I create nodes (1-6) I can run your command and I'm back to 1.  Previously, I was never able to delete all of my data for some reason.  There must have been something funky going on.

Just an FYI, I do not believe the statement you said is accurate about the webadmin-monitoring.  After I created the 6 nodes (webadmin showed 7), I deleted nodes 2-4 and webadmin showed 4.

Thanks for all of your help.

Michael Hunger

unread,
Sep 20, 2012, 5:11:44 PM9/20/12
to ne...@googlegroups.com
You're welcome.

have fun and thanks for the feedback.

Michael

--
 
 

Larry Hengl

unread,
Sep 30, 2013, 1:31:37 AM9/30/13
to ne...@googlegroups.com, ni...@nigelsmall.net
hi gang, i'm seeing this issue too.  playing with py2neo v1.6 and neo4j v2 m5.
i am testing with the py2neo.clear()  db wiping method, and it works (although it seems to wipe the ref node too). when i load a set of of nodes and then clear() to re-test another set, they are correctly removed. but the web admin keeps incrementing the summary of nodes. in my particular case, i have ~400 nodes shown via cypher in the web admin console and shell, but the web admin summary of nodes shows well over 1200 nodes, after a few test runs. i even restarted the server.  looks like it shows the last id used??

seems like a neo4j issue, not necessarily a py2neo issue...but i'm not sure.

thoughts?

thx,
larry

Michael Hunger

unread,
Sep 30, 2013, 2:09:37 AM9/30/13
to ne...@googlegroups.com
looks like it shows the last id used??

Exactly, highest id generated.

Sent from mobile device

Nigel Small

unread,
Sep 30, 2013, 2:13:59 AM9/30/13
to Neo4J
Also, I decided that clear should erase all nodes, including the reference node, for two reasons:
  1. The reference node is deprecated now so should not be used
  2. The only way to perform a clear is through Cypher which does not have a way to specifically exclude the reference node anyway


--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Larry Hengl

unread,
Sep 30, 2013, 3:07:21 AM9/30/13
to ne...@googlegroups.com
hmmm.  seems misleading to show X nodes in the web admin summary (reflecting last id) when there may be a completely different node count, Y.  is the an intended "feature" of the web admin dashboard?

not a big deal, as I'm using cypher to produce my aggregations anyway.

thx for making Neo4j awesome.

larry

Larry Hengl

unread,
Sep 30, 2013, 3:13:28 AM9/30/13
to ne...@googlegroups.com
thanks Nigel.  sounds good to me.

another quick note regarding the new add_labels... i was trying to run a batch create, but couldn't find a clear way to add labels in that process (maybe over thinking it).  but then tried to create single abstract nodes to add labels to, and that doesn't work either.  only way is to create a Node, and then add a label(s) afterwards. i was hoping to shove properties and labels into a single batch create step, but so far that isn't possible?  perhaps this is another thread topic.

awesome lib!  many thanks!

larry

Michael Hunger

unread,
Sep 30, 2013, 3:15:42 AM9/30/13
to ne...@googlegroups.com
This screen in the web-ui has been a nuisance forever.
Will get better in Neo4j 2.0, promised.

Michael

Nigel Small

unread,
Sep 30, 2013, 4:42:57 AM9/30/13
to Neo4J
Hi Larry

Py2neo can only work with the the API as exposed by the underlying server and there is no single call available to create a node with labels attached.

You could look at a batched Cypher query to do this in one step (http://book.py2neo.org/en/latest/batches/#py2neo.neo4j.ReadBatch.append_cypher) but bear in mind that return values from such queries cannot be referred to in other batch requests using the numeric reference mechanism.

Nige
Reply all
Reply to author
Forward
0 new messages