Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

How to delete unwanted nodes using custom code.

Avatar

Level 4

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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()

 

View solution in original post

11 Replies

Avatar

Community Advisor

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

 

I hope it helps.

Avatar

Level 4

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

Avatar

Correct answer by
Community Advisor

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()

 

Avatar

Community Advisor

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

Avatar

Community Advisor

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.

 

Avatar

Level 4

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.

Avatar

Community Advisor

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

Avatar

Level 4

I have tried that but it's not giving the exact results.

Ex:/content/company/language/en/home/jcr:content/root/responsivegrid/text

/content/company/language/en/home/jcr:content/root/responsivegrid/text/cq:responsive

Avatar

Level 5

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/

Avatar

Community Advisor

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

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