Expand my Community achievements bar.

SOLVED

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

Avatar

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

Avatar

Correct answer by
Employee 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

Avatar

Community Advisor

Hi,

To remove the property you need to use

node.setProperty(propertyName, null



Arun Patidar

Avatar

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!

Avatar

Correct answer by
Employee 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