How to delete unwanted nodes using custom code. | Community
Skip to main content
Level 3
March 3, 2023
Solved

How to delete unwanted nodes using custom code.

  • March 3, 2023
  • 5 replies
  • 6539 views

Hi, In our project we have "n" number of nodes which does not have sling:resourceType and it's caussing below error in logs.org.apache.sling.servlets.get.impl.DefaultGetServlet No renderer for extension html, cannot render resource JcrNodeResource, type=nt:unstructured, superType=null. Please suggest how can we handle this.

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 Nitin_laad

Hi @vijitha - you can write something like - if you have list of nodes, you can create an array of it or you can read it from csv

 

import javax.jcr.Node import javax.jcr.Session // Array of node paths to delete def pathsToDelete = [ "/content/company/language/en/home/jcr:content/root/responsivegrid/text", "/content/company/language/en/home/jcr:content/root/responsivegrid/text/test", "/content/company/language/en/support/jcr:content/root/responsivegrid/image", "/content/company/language/en/build/jcr:content/root/responsivegrid/cta" ] // Get the current session def session = resourceResolver.adaptTo(Session) // Loop through the paths and delete the nodes pathsToDelete.each { path -> Node node = session.getNode(path) node.remove() } // Save the changes to the session session.save()

 

5 replies

Rohit_Utreja
Community Advisor
Community Advisor
March 3, 2023

@vijitha 

It can be done by using Node API.

You can right a servlet to pass the parent node path on temporary basis. Later, this servlet can be removed from code repo.

First a parent node path would be required and then all child nodes can be iterated using node api. 

Then, you can check if the node has  sling:resourceType property or not. If it's not there, you can remove it.

Here, is one similar thread.

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-remove-childnode-in-aem/td-p/321560

 

I hope it helps.

vijithaAuthor
Level 3
March 3, 2023

Hi Rohit,

Thanks for your reply.I have simliar issue for "n" number of nodes.How can we give parent node path for this.

Few Examples:

/content/company/language/en/home/jcr:content/root/responsivegrid/text ---I need to delete text node for this

/content/company/language/en/home/jcr:content/root/responsivegrid/text/test---I need to delete test node for this

/content/company/language/en/support/jcr:content/root/responsivegrid/image---I need to delete image node for this

/content/company/language/en/build/jcr:content/root/responsivegrid/cta---I need to delete cta node for this

Nitin_laad
Community Advisor
Nitin_laadCommunity AdvisorAccepted solution
Community Advisor
March 3, 2023

Hi @vijitha - you can write something like - if you have list of nodes, you can create an array of it or you can read it from csv

 

import javax.jcr.Node import javax.jcr.Session // Array of node paths to delete def pathsToDelete = [ "/content/company/language/en/home/jcr:content/root/responsivegrid/text", "/content/company/language/en/home/jcr:content/root/responsivegrid/text/test", "/content/company/language/en/support/jcr:content/root/responsivegrid/image", "/content/company/language/en/build/jcr:content/root/responsivegrid/cta" ] // Get the current session def session = resourceResolver.adaptTo(Session) // Loop through the paths and delete the nodes pathsToDelete.each { path -> Node node = session.getNode(path) node.remove() } // Save the changes to the session session.save()

 

aanchal-sikka
Community Advisor
Community Advisor
March 3, 2023

Hello @vijitha 

 

It can be done via Groovy script.

1. You would need to traverse the pages and configured components hierarchically.

2. Check if the sling:resourceType exists. If not, delete the node.

 

You would have to append /apps to sling:resourceType to check for existence. A similar script for visiting sling:resourceType is shared on SOLVED: Find all the components on the current pag... - Adobe Experience League Community - 577223

Aanchal Sikka
Sady_Rifat
Community Advisor
Community Advisor
March 3, 2023

Hello @vijitha ,
To delete the node which doesn't have sling:resourceType you can run query builder. Here is a sample query builder ->

 

path=/content/company/language/en type=nt:unstructured 1_property=sling:resourceType 1_property.operation=not 2_property=jcr:created 2_property.operation=exists p.limit=-1

2_property is added to find out the component. Otherwise, it will remove all the nodes.
Once you get the list run a loop and resolve to Node type and delete.
Deleting node from Java will be like

// RUN querybuilder code

String rootPath = "/content/company/language/en";
ResourceResolver
resourceResolver = getRequest().getResourceResolver();
Node rootNode = resourceResolver.resolve(rootPath).adaptTo(Node.class);

//Iterate over querybuilder result and get the PATH
Node faultyNode = rootNode.getNode(PATH.replace(rootPath, ""));
faultyNode.remove();
// Loop Scope end

rootNode.getSession().save();

Hope it will help you.

 

vijithaAuthor
Level 3
March 7, 2023

Hi @sady_rifat 

Thanks for your reply I have tried with your query in local It's working fine but in higher envronaments I don't see 2_property=jcr:created node in my code base.

I see only cq:lastRolledoutBy and  cq:lastRolledout properties for /content/company/language/en/home/jcr:content/root/responsivegrid/text so I am not able to get the exact results.

Sady_Rifat
Community Advisor
Community Advisor
March 7, 2023

So, if you use 2_property=cq:lastRolledoutBy or 2_property=cq:lastRolledout, this should work as expected. Aren't you?

Level 4
March 3, 2023

To delete unwanted nodes you can follow below steps

 

1. Identify the nodes using the querybuilder and list out all the possible node paths and export them to the Excel.

2. Install groovy console in the instance and give required permission to the user to run the script

3. Upload the excel in your DAM location and point that in your groovy script

4. Run the script with dry run flag and check using print method in groovy that all the nodes are iterated properly.

5. Then enable actual run and execute the script.

Below is the link which provides the details of groovy and you can see some samples as well in the console dashboard

Groovy console Link:

https://aemgeeks.com/aem-tools/use-groovy-script-in-aem/

DPrakashRaj
Community Advisor
Community Advisor
March 3, 2023

deleting a node programmatically, please follow https://sling.apache.org/documentation/the-sling-engine/sling-api-crud-support.html#delete-a-resource

with script https://dileepakv.blogspot.com/2017/12/bulk-delete-and-package-paths-in-aem.html

 

Things to keep in mind while making those changes that not all node in aem normally requires sling:resourceType so to get the list of nodes who are problematics you need to write some code may be node traversal to check with node path for all component normally last node name in the node path matches to the component name.

Few Examples:

/content/company/language/en/home/jcr:content/root/responsivegrid/text ---I if this node doesn't have any sling:resourceType you need to delete this one as it will be referencing to text component

 

Hope that helps