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
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Unable to remove or delete a page property using Groovy Script.

samikshaa223429
Level 3
Level 3

hi,

 

I have a requirement where I need to update the page property type from String to String[]. 

Solution 1: BULK Editor 

I tried using the bulk editor extension provided in the link below, which provides a custom extension to the bulk editor tool to allow saving properties of type String[], but it failed to work.

LINK --> https://www.tothenew.com/blog/aemcq5-6-bulk-editor-extension-part-1/ 

AEM Version -> 6.5 

 

Solution 2: Using Groovy script. 

Is there a way we can remove a page property in groovy? and then I can create a String array and re-create the same property using the code below..

 if(it.hasProperty(propertyName1))
{
String v = it.get(propertyName1);
println v;
//it.remove(propertyName1);
final String[] regionArray = [v];
it.setProperty(propertyName1,regionArray);
count++;
println 'Node with Form Path =' + nodePath + ' with property '+ propertyName1 +' = ' + it.get(propertyName1);
}

 

However it.remove doesnt seem to work .

 

Solution 3: TYPE Hint 

Tried using typeHInt , but would want it to apply on all existing pages using the property as well.. so not feasible

 

Need help to figure out a solution.

 

Thanks,

Samiksha

 

1 Accepted Solution
Bimmi_Soi
Correct answer by
Community Advisor
Community Advisor

Hi @samikshaa223429 ,

 

Setting the property to null will remove that property. You can do as below

node.setProperty(propertyName,null);

 

or use remove method

node.getProperty(propertyName).remove();

 

But dont forget to save it 

session.save();

 

And I think you are missing session.save(); in your code.

 

Hope this helps!!
Thanks

 

View solution in original post

3 Replies
Arun_Patidar
Community Advisor
Community Advisor

Hi,

To remove the property you need to use

node.setProperty(propertyName, null

Dipti_Chauhan
Community Advisor
Community Advisor

Hi @samikshaa223429 

  You can use remove method also.

Sharing one sample code for property delete:

 

 

Node n = getNode('path');
n.getProperty('title').remove();
save()

 

Hope this helps!

Bimmi_Soi
Correct answer by
Community Advisor
Community Advisor

Hi @samikshaa223429 ,

 

Setting the property to null will remove that property. You can do as below

node.setProperty(propertyName,null);

 

or use remove method

node.getProperty(propertyName).remove();

 

But dont forget to save it 

session.save();

 

And I think you are missing session.save(); in your code.

 

Hope this helps!!
Thanks

 

View solution in original post