Remove list of pages using groovy script | Community
Skip to main content
gayatrik8153299
Level 2
Sep 10, 2020 1:27 PM UTC
Solved

Remove list of pages using groovy script

Hi All,

We have some list of pages in an array and need to remove all those pages from the list.

Ex:

def list =["/content/we-retail/us/en/test1","/content/we-retail/be/en/test1","/content/we-retail/us/en/test2","/content/we-retail/be/en/test2"]

 

We tried with below code but it is not working.Could you please assist on this asap.

 

def buildQuery(list)
{
def queryManager = session.workspace.queryManager
def statement = 'SELECT * FROM [nt:unstructured] AS node WHERE ISDESCENDANTNODE (node, "/content/we-retail") and name()=\'' + list+ '\'';
queryManager.createQuery(statement, 'sql')
}
def list =["/content/we-retail/us/en/test1","/content/we-retail/be/en/test1","/content/we-retail/us/en/test2","/content/we-retail/be/en/test2"]
final def query = buildQuery(list)
final def result = query.execute()
if(result.nodes.size()!=0){
result.nodes.each { node ->
if(node.contains(list))
{
node.remove()
save()
println "removed:"+list
}
}
}

 

@kautuk_sahni 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
aemmarc2Accepted solution
Employee
Sep 10, 2020 3:03 PM UTC

3 replies

arunpatidar
New Participant
Sep 13, 2020 5:33 PM UTC
aemmarc2Accepted solution
Employee
Sep 10, 2020 3:03 PM UTC

Tried, tested and true --> https://gist.github.com/stillalex/43c49af065e3dd1fd5bf

Translate
Andrei_Dantsou
Level 3
Sep 10, 2020 1:59 PM UTC

Hello @gayatrik8153299 ,

If your goal is to remove pages by the paths from the list, you can just iterate through the list with paths, get nodes and remove them (no querying is required):

 

def list =["/content/we-retail/us/en/test1","/content/we-retail/be/en/test1","/content/we-retail/us/en/test2","/content/we-retail/be/en/test2"] list.each { path -> def node = getNode(path); node.remove(); println "removed:"+path } session.save()

 

 

or using Resource API:

 

def list =["/content/we-retail/us/en/test1","/content/we-retail/be/en/test1","/content/we-retail/us/en/test2","/content/we-retail/be/en/test2"] list.each { path -> Resource resource = resourceResolver.getResource(path); if (resource != null) { resourceResolver.delete(resource); println "removed:"+path } } resourceResolver.commit();

 

Regards

Translate