How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!
While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.
// This script was created to restore database consistency after // unintentionally deleting necessary entries from the dlFolder table.
// The repository table has orphaned entries pointing to nonexisting // dlFolderId's. // Note: This script has a flag to execute in safe mode. // By default, runDry = true is set to allow users to check if // entries appearing in the log correspond to orphaned entries // pointing to nonexisting dlFolderId's.
import com.liferay.portal.kernel.service.RepositoryLocalServiceUtil;
import com.liferay.portal.kernel.model.Repository;
import com.liferay.portal.kernel.dao.jdbc.DataAccess;
import com.liferay.portal.kernel.util.PortalUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
boolean runDry = true;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
if (runDry) {
out.println("Executing in save mode. No removals will be performed.");
}
try {
con = DataAccess.getConnection();
String sql = "select repositoryId from repository where name like 'com.liferay.portal.kernel.util.TempFileEntryUtil' and dlFolderId not in (select folderId from dlfolder);";
sql = PortalUtil.transformSQL(sql);
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
int rows = 0;
while (rs.next()) {
long repositoryId = rs.getLong(1);
if (!runDry) {
try {
RepositoryLocalServiceUtil.deleteRepository(repositoryId);
out.println("Succesfully deleted repository: " + repositoryId);
} catch(Exception e) {
e.printStackTrace(out);
}
} else {
out.println("RepositoryId " + repositoryId + " would be removed.");
}
rows++;
}
if (!runDry) {
out.println(rows + " rows have been removed.");
} else {
out.println(rows + " rows would have been removed.");
}
}
catch(Exception e) {
e.printStackTrace(out);
}