Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
In our case, there were 45 legacy / lost dlfileentries in our system with no corresponding dlfileversion record. Gather a list of the offending fileEntryIds and use this script to remove them. This script needs to be run before the mass document version cleanup can occur.
// Delete files that have no dlfileversion record...
// Use this query to find them...:
// select fileEntryId from dlfileentry where fileEntryId NOT IN
// (select dlfe.fileEntryId from dlfileentry dlfe, dlfileversion dlfv where dlfe.fileEntryId=dlfv.fileEntryId and dlfv.version=dlfe.version);
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import java.util.List;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.repository.model.FileVersion;
import com.liferay.portal.kernel.repository.model.Folder;
import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
import com.liferay.portlet.documentlibrary.model.DLFileVersion;
import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
previewMode = true;
if(previewMode) {
out.println(
"""<div class="portlet-msg-alert">Preview mode is on: switch off the flag and execute this script
again to make changes to the database</div>""")
}
def SCRIPT_ID = "Delete-Files-w-No-Version"
outputFile = new File(
"""${System.getProperty("liferay.home")}/scripting/out-${SCRIPT_ID}.txt""")
outputFile.getParentFile().mkdirs()
def trace(message) {
out.println(message)
outputFile << "${message}\n"
}
try {
def allfiles = '1302031,1302076,1302081,1325718,1325763,1675804,1840601,1872535,1919354,\
1937807,1937812,1937816,1937946,1938015,1938020,1940276,1940280,1940285,\
2218319,2218324,2218328,2218332,2218336,2218340,2218346,2218350,2218356,\
2218360,2218364,2218370,2218374,2288427,2288452,2288459,2288517,2297297,\
3657858,3721708,5160026,7725031,7725146,8311008,9020340,9020431,9105681';
String[] filesToWhack = allfiles.split(',')
for(i=0;i<filesToWhack.size();i++){
thisFile = filesToWhack[i]
thisFile = Integer.parseInt(thisFile)
trace('Deleting: ' + 'fileEntryId=' + thisFile)
if(!previewMode) {
DLAppLocalServiceUtil.deleteFileEntry(thisFile);
}
}
} catch (Exception e) {
println(e)
e.printStackTrace(out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment