Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How to delete node using java code

Avatar

Level 5

I have 2-3 component node which are not required. I am writing workflow to check those node. How should I delete the node using Java.

 

Eg: 

Node1- Example

Node2- Test

 

If I find the resource.getName() as Node1 delete that node 

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi Ronnie,

 

You can use the delete method https://sling.apache.org/apidocs/sling7/org/apache/sling/api/resource/ResourceResolver.html#delete-o...

 

Alternatively, you can use - https://docs.adobe.com/content/docs/en/spec/jcr/1.0/7.1.6_Removing_Nodes_and_Properties.html

javax.jcr.Node jcrNode = jcrsession.getNode("/sites/mySite");
jcrNode.remove();
jcrsession.save();

 

Regards,

Manpreet

View solution in original post

3 Replies

Avatar

Correct answer by
Level 4

Hi Ronnie,

 

You can use the delete method https://sling.apache.org/apidocs/sling7/org/apache/sling/api/resource/ResourceResolver.html#delete-o...

 

Alternatively, you can use - https://docs.adobe.com/content/docs/en/spec/jcr/1.0/7.1.6_Removing_Nodes_and_Properties.html

javax.jcr.Node jcrNode = jcrsession.getNode("/sites/mySite");
jcrNode.remove();
jcrsession.save();

 

Regards,

Manpreet

Avatar

Community Advisor

Hi @Ronnie09 ,

         You need to used Node api for adding and removing .

Using Node API

Adapt the resource to Node

Node node = resource.adaptTo(Node.class);

Then remove a node using function removenode(java.lang.String relPath, java.lang.String primaryNodeTypeName)"

  • node.remove(nodeName, NodePrimaryType);
  • you can add properties using function "setProperty(java.lang.String name,Value value)"
  • Save the session so that the new Node and its properties are saved

Kr,

Sanjay