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);
}
}
Solved! Go to Solution.
Views
Replies
Total Likes
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); }
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
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.
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); }