I tried deleting one node and iterating forward it gives error | Community
Skip to main content
Level 6
Oct 1, 2021 2:48 PM UTC
Solved

I tried deleting one node and iterating forward it gives error

I tried the below code but it gives error NODE DOESN'T EXIST ANY MORE

 

 

public void deleteNode(Resource resource, Node n) throws RepositoryException { int flag=0; if("/".equals(n.getPath())) { return; } if(Objects.nonNull(resource) && resource.hasChildren()) { for(Map.Entry delete:deleteNode.entrySet()) { String delPath[]=delete.getKey().toString().split("="); boolean value=(boolean) delete.getValue(); LOG.info("NON DELETION is "+delPath[0]+" should be "+delPath[1]+" "+value); if (n.getPath().equals(delPath[0])||n.getName().equals(launchName)|| n.getName().equals("cq:LiveSyncConfig")||n.getName().equals("root")||n.getName().equals("responsivegrid")) {flag++;} } } //n.getSession().save(); final NodeIterator it = n.getNodes(); while(it.hasNext()) { final Node next = it.nextNode(); if(flag==0) { LOG.info("DELETING NODE "+n.getPath()); n.remove(); n.getSession().save(); } deleteNode(resource,next); } }

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Ronnie09Accepted solution
Level 6
Oct 4, 2021 8:48 AM UTC
 	 if(flag==0)
		 {
			 LOG.info("DELETING NODE "+n.getPath());
			n.remove();
			n=resource.adaptTo(Node.class);
			n.getSession().save();
		 }

   	 final NodeIterator it = n.getNodes();
        while(it.hasNext()) {
            final Node next = it.nextNode();
           
            deleteNode(resource,next);
        }
   	 

3 replies

Ronnie09AuthorAccepted solution
Level 6
Oct 4, 2021 8:48 AM UTC
 	 if(flag==0)
		 {
			 LOG.info("DELETING NODE "+n.getPath());
			n.remove();
			n=resource.adaptTo(Node.class);
			n.getSession().save();
		 }

   	 final NodeIterator it = n.getNodes();
        while(it.hasNext()) {
            final Node next = it.nextNode();
           
            deleteNode(resource,next);
        }
   	 
Translate
Shubham_borole
New Participant
Oct 3, 2021 2:19 PM UTC

@ronnie09 

Hi It seems to me we are iterating child nodes of node 'n' and instead of doing next.remove() we are doing n.remove(). Once the parent node is removed there won't be anything to iterate forward.

Translate
Ravi_Pampana
New Participant
Oct 2, 2021 5:15 PM UTC

Hi,

 

Check if you are deleting the parent node and trying to delete the child node. Once parent node is deleted all child nodes will be deleted. Based on the exception which you are getting seems the parent node is getting deleted first and you are trying to delete child node. 

 

Note: NodeIterator is present before delete it will have all the nodes 

Translate