How to delete a node in Neo4j using py2neo?












0















Using py2neo v4 to connect to my Neo4j database and I can't delete nodes via py2neo running a query that works fine in Cypher in the browser. Of course there is no real documentation for either Neo4j or py2neo, so hopefully I can get some help here. There are similar questions, but both Neo4j and py2neo have new versions since then, and those questions/answers are either for other specific cases or are obsolete methods.



First, I define this function:



def deleteNode(thisNodeID):
graph.run("MATCH (n) where id(n) = $nodeID DETACH DELETE n",
parameters={"nodeID":thisNodeID})


Then I call the function like:



badObjectIDs = [268569,268535,268534]
for badID in badObjectIDs:
deleteNode(badID)


This runs without any trouble, but doesn't delete anything and the nodes with those IDs are still in the database when I search via the browser.



I also tried using py2neo's graph.delete() method, but again I wasn't able to get anything to work because there is no description or examples in the documentation to get it to work. I couldn't even find a way to get nodes by IDs in the documentation. Somthing like



graph.delete(matcher.match("Person"))


should delete all the nodes with the "Person" label, but instead it throws an error



TypeError: No method defined to delete object <py2neo.matching.NodeMatch object at 0x0000026F52A8DC50>


So it's really just a basic question in using py2neo that should be explained clearly in the documentation or beginner tutorials, but again, there are no examples of using any of these methods anywhere I could find.





How do I remove nodes from my Neo4j database using py2neo?












share|improve this question



























    0















    Using py2neo v4 to connect to my Neo4j database and I can't delete nodes via py2neo running a query that works fine in Cypher in the browser. Of course there is no real documentation for either Neo4j or py2neo, so hopefully I can get some help here. There are similar questions, but both Neo4j and py2neo have new versions since then, and those questions/answers are either for other specific cases or are obsolete methods.



    First, I define this function:



    def deleteNode(thisNodeID):
    graph.run("MATCH (n) where id(n) = $nodeID DETACH DELETE n",
    parameters={"nodeID":thisNodeID})


    Then I call the function like:



    badObjectIDs = [268569,268535,268534]
    for badID in badObjectIDs:
    deleteNode(badID)


    This runs without any trouble, but doesn't delete anything and the nodes with those IDs are still in the database when I search via the browser.



    I also tried using py2neo's graph.delete() method, but again I wasn't able to get anything to work because there is no description or examples in the documentation to get it to work. I couldn't even find a way to get nodes by IDs in the documentation. Somthing like



    graph.delete(matcher.match("Person"))


    should delete all the nodes with the "Person" label, but instead it throws an error



    TypeError: No method defined to delete object <py2neo.matching.NodeMatch object at 0x0000026F52A8DC50>


    So it's really just a basic question in using py2neo that should be explained clearly in the documentation or beginner tutorials, but again, there are no examples of using any of these methods anywhere I could find.





    How do I remove nodes from my Neo4j database using py2neo?












    share|improve this question

























      0












      0








      0








      Using py2neo v4 to connect to my Neo4j database and I can't delete nodes via py2neo running a query that works fine in Cypher in the browser. Of course there is no real documentation for either Neo4j or py2neo, so hopefully I can get some help here. There are similar questions, but both Neo4j and py2neo have new versions since then, and those questions/answers are either for other specific cases or are obsolete methods.



      First, I define this function:



      def deleteNode(thisNodeID):
      graph.run("MATCH (n) where id(n) = $nodeID DETACH DELETE n",
      parameters={"nodeID":thisNodeID})


      Then I call the function like:



      badObjectIDs = [268569,268535,268534]
      for badID in badObjectIDs:
      deleteNode(badID)


      This runs without any trouble, but doesn't delete anything and the nodes with those IDs are still in the database when I search via the browser.



      I also tried using py2neo's graph.delete() method, but again I wasn't able to get anything to work because there is no description or examples in the documentation to get it to work. I couldn't even find a way to get nodes by IDs in the documentation. Somthing like



      graph.delete(matcher.match("Person"))


      should delete all the nodes with the "Person" label, but instead it throws an error



      TypeError: No method defined to delete object <py2neo.matching.NodeMatch object at 0x0000026F52A8DC50>


      So it's really just a basic question in using py2neo that should be explained clearly in the documentation or beginner tutorials, but again, there are no examples of using any of these methods anywhere I could find.





      How do I remove nodes from my Neo4j database using py2neo?












      share|improve this question














      Using py2neo v4 to connect to my Neo4j database and I can't delete nodes via py2neo running a query that works fine in Cypher in the browser. Of course there is no real documentation for either Neo4j or py2neo, so hopefully I can get some help here. There are similar questions, but both Neo4j and py2neo have new versions since then, and those questions/answers are either for other specific cases or are obsolete methods.



      First, I define this function:



      def deleteNode(thisNodeID):
      graph.run("MATCH (n) where id(n) = $nodeID DETACH DELETE n",
      parameters={"nodeID":thisNodeID})


      Then I call the function like:



      badObjectIDs = [268569,268535,268534]
      for badID in badObjectIDs:
      deleteNode(badID)


      This runs without any trouble, but doesn't delete anything and the nodes with those IDs are still in the database when I search via the browser.



      I also tried using py2neo's graph.delete() method, but again I wasn't able to get anything to work because there is no description or examples in the documentation to get it to work. I couldn't even find a way to get nodes by IDs in the documentation. Somthing like



      graph.delete(matcher.match("Person"))


      should delete all the nodes with the "Person" label, but instead it throws an error



      TypeError: No method defined to delete object <py2neo.matching.NodeMatch object at 0x0000026F52A8DC50>


      So it's really just a basic question in using py2neo that should be explained clearly in the documentation or beginner tutorials, but again, there are no examples of using any of these methods anywhere I could find.





      How do I remove nodes from my Neo4j database using py2neo?









      python neo4j py2neo






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 8:53









      Aaron BramsonAaron Bramson

      339517




      339517
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I was able to delete a node, with ID=20 like this:



          from py2neo import Graph, Node, Relationship

          # Create graph
          graph = Graph(host="localhost", auth=("neo4j", <insert_password>))

          # Create nodes
          nicole = Node('Person', name='Nicole')
          adam = Node('Person', name='Adam')

          # Create relationship between 2 nodes
          graph.create(Relationship(nicole, 'KNOWS', adam))

          # Select node with id = 20
          id_20 = graph.evaluate("MATCH (n) where id(n) = 20 RETURN n")

          # Delete node
          graph.delete(id_20)


          As for the function, it should work with something like this:



          def deleteNode(id):
          node = graph.evaluate("MATCH (n) where id(n) = {} RETURN n".format(id))
          graph.delete(node)


          You can yield the id of any node IN THE GRAPH by doing this:



          node = graph.evaluate("MATCH (n) where id(n) = {} RETURN n".format(id))
          node.identity


          Just to be clear, I'm using neo4j-driver version 1.6.2






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389323%2fhow-to-delete-a-node-in-neo4j-using-py2neo%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I was able to delete a node, with ID=20 like this:



            from py2neo import Graph, Node, Relationship

            # Create graph
            graph = Graph(host="localhost", auth=("neo4j", <insert_password>))

            # Create nodes
            nicole = Node('Person', name='Nicole')
            adam = Node('Person', name='Adam')

            # Create relationship between 2 nodes
            graph.create(Relationship(nicole, 'KNOWS', adam))

            # Select node with id = 20
            id_20 = graph.evaluate("MATCH (n) where id(n) = 20 RETURN n")

            # Delete node
            graph.delete(id_20)


            As for the function, it should work with something like this:



            def deleteNode(id):
            node = graph.evaluate("MATCH (n) where id(n) = {} RETURN n".format(id))
            graph.delete(node)


            You can yield the id of any node IN THE GRAPH by doing this:



            node = graph.evaluate("MATCH (n) where id(n) = {} RETURN n".format(id))
            node.identity


            Just to be clear, I'm using neo4j-driver version 1.6.2






            share|improve this answer






























              0














              I was able to delete a node, with ID=20 like this:



              from py2neo import Graph, Node, Relationship

              # Create graph
              graph = Graph(host="localhost", auth=("neo4j", <insert_password>))

              # Create nodes
              nicole = Node('Person', name='Nicole')
              adam = Node('Person', name='Adam')

              # Create relationship between 2 nodes
              graph.create(Relationship(nicole, 'KNOWS', adam))

              # Select node with id = 20
              id_20 = graph.evaluate("MATCH (n) where id(n) = 20 RETURN n")

              # Delete node
              graph.delete(id_20)


              As for the function, it should work with something like this:



              def deleteNode(id):
              node = graph.evaluate("MATCH (n) where id(n) = {} RETURN n".format(id))
              graph.delete(node)


              You can yield the id of any node IN THE GRAPH by doing this:



              node = graph.evaluate("MATCH (n) where id(n) = {} RETURN n".format(id))
              node.identity


              Just to be clear, I'm using neo4j-driver version 1.6.2






              share|improve this answer




























                0












                0








                0







                I was able to delete a node, with ID=20 like this:



                from py2neo import Graph, Node, Relationship

                # Create graph
                graph = Graph(host="localhost", auth=("neo4j", <insert_password>))

                # Create nodes
                nicole = Node('Person', name='Nicole')
                adam = Node('Person', name='Adam')

                # Create relationship between 2 nodes
                graph.create(Relationship(nicole, 'KNOWS', adam))

                # Select node with id = 20
                id_20 = graph.evaluate("MATCH (n) where id(n) = 20 RETURN n")

                # Delete node
                graph.delete(id_20)


                As for the function, it should work with something like this:



                def deleteNode(id):
                node = graph.evaluate("MATCH (n) where id(n) = {} RETURN n".format(id))
                graph.delete(node)


                You can yield the id of any node IN THE GRAPH by doing this:



                node = graph.evaluate("MATCH (n) where id(n) = {} RETURN n".format(id))
                node.identity


                Just to be clear, I'm using neo4j-driver version 1.6.2






                share|improve this answer















                I was able to delete a node, with ID=20 like this:



                from py2neo import Graph, Node, Relationship

                # Create graph
                graph = Graph(host="localhost", auth=("neo4j", <insert_password>))

                # Create nodes
                nicole = Node('Person', name='Nicole')
                adam = Node('Person', name='Adam')

                # Create relationship between 2 nodes
                graph.create(Relationship(nicole, 'KNOWS', adam))

                # Select node with id = 20
                id_20 = graph.evaluate("MATCH (n) where id(n) = 20 RETURN n")

                # Delete node
                graph.delete(id_20)


                As for the function, it should work with something like this:



                def deleteNode(id):
                node = graph.evaluate("MATCH (n) where id(n) = {} RETURN n".format(id))
                graph.delete(node)


                You can yield the id of any node IN THE GRAPH by doing this:



                node = graph.evaluate("MATCH (n) where id(n) = {} RETURN n".format(id))
                node.identity


                Just to be clear, I'm using neo4j-driver version 1.6.2







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 25 '18 at 20:15

























                answered Dec 25 '18 at 19:57









                Alex RamsesAlex Ramses

                85




                85
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389323%2fhow-to-delete-a-node-in-neo4j-using-py2neo%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?