I tried deleting one node and iterating forward it gives error | Community
Skip to main content
Level 6
October 1, 2021
Solved

I tried deleting one node and iterating forward it gives error

  • October 1, 2021
  • 3 replies
  • 1276 views

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.
Best answer by Ronnie09
 	 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

Ravi_Pampana
Community Advisor
Community Advisor
October 2, 2021

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 

Shubham_borole
Community Advisor
Community Advisor
October 3, 2021

@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.

Ronnie09AuthorAccepted solution
Level 6
October 4, 2021
 	 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);
        }