Script - Compact documents with size equals to 0

Find all documents with size=0 and compact history of versions to latest.

import java.util.*;
import com.openkm.api.OKMDocument;
import com.openkm.api.OKMRepository;
import com.openkm.dao.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import com.openkm.dao.bean.NodeDocumentVersion;
 
String qs = "from NodeDocumentVersion ndv where ndv.size=0";
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Query q = session.createQuery(qs);
List docVersionList = q.list();
HibernateUtil.commit(tx);
 
print("Number of nodes: "+docVersionList.size()+"<br/>");
 
for (NodeDocumentVersion ndv : docVersionList) {
    String path = OKMRepository.getInstance().getNodePath(null,ndv.getParent());
    OKMDocument.getInstance().purgeVersionHistory(null, path);
}
 
HibernateUtil.close(session);
print("done");