System Administrator Jahia 7.3 Jahia 8 Legacy

Deleting a node or a site with a script

Question

How to delete a node or a site with Groovy script?

Answer

There are different ways to delete content in Jahia such as using the UI, JCR browser or the REST API as shown in our documentation on Content manipulation. Below is a simple script to delete a node (or even an entire site) using a groovy script:

org.apache.jackrabbit.core.JahiaRepositoryImpl rep = org.jahia.services.SpringContextSingleton.getBean("jackrabbit").getRepository();
javax.jcr.SimpleCredentials cred = (javax.jcr.SimpleCredentials)org.apache.jackrabbit.core.security.JahiaLoginModule.getSystemCredentials("system");
cred.setAttribute("org.apache.jackrabbit.autoFixCorruptions", "true");
javax.jcr.Session jcrsession = rep.login(cred, "default");
javax.jcr.Node jcrNode = jcrsession.getNode("/sites/mySite");
jcrNode.remove();
jcrsession.save();
 A backup is strongly recommended before executing any update or delete query