Skip to content

Instantly share code, notes, and snippets.

@aitseitz
Created January 26, 2024 08:45
Show Gist options
  • Save aitseitz/02e9cf62ab0060440b456ba9566d25ee to your computer and use it in GitHub Desktop.
Save aitseitz/02e9cf62ab0060440b456ba9566d25ee to your computer and use it in GitHub Desktop.
Cleanup Alfresco's Lost&Found Folder in the Repository
#!/bin/bash
echo "$(date +"%Y-%m-%d %H:%M:%S,%3N") - This Script automatically cleanups the nodes underneath Lost & Found folder"
# Variables:
ALFRESCO_HOST_URL='https://localhost:8443'
AUTH_PASS="YWRtaW46YWRtaW4="
LOST_N_FOUND_FOLDER_NODE_ID="2f849cd8-9737-477d-9584-1af99a3b4b0e"
# Optional Variables
MAX_ITEMS=1000
WAIT_TIME=.2 # option to set a wait in seconds time to prevent to many delete api calls in production
DELETE_PERMANENT=true
DIRECTORY=$(pwd)
LOG_DATE=$(date '+%Y-%m-%d_%H-%M')
LOG_NAME=$(basename $0)
LOGFILE="${DIRECTORY}/${LOG_DATE}_${LOG_NAME}.log"
OUTPUT_FILE="${DIRECTORY}/${LOG_DATE}_${LOG_NAME}.json"
if ! [ -d "${LOGFILE}" ];then
touch "${LOGFILE}"
echo "$(date +"%Y-%m-%d %H:%M:%S,%3N") - Logfile created: $LOGFILE"
fi
function saveChildrenNodeIDsToOutputfile(){
curl -X GET "${ALFRESCO_HOST_URL}/alfresco/api/-default-/public/alfresco/versions/1/nodes/${LOST_N_FOUND_FOLDER_NODE_ID}/children?skipCount=0&maxItems=${MAX_ITEMS}" \
-H "accept: application/json" \
-H "authorization: Basic ${AUTH_PASS}" \
--output ${OUTPUT_FILE}
#--compressed
# Filter Children Node UUIDs from result:
CHILDREN_NODE_IDS=$(jq -r '.list.entries[].entry.id' "${OUTPUT_FILE}")
}
function deleteNodeID(){
DELETE_NODE_ID=$1
echo "$(date +"%Y-%m-%d %H:%M:%S,%3N") - API-Call to delete nodeid: ${DELETE_NODE_ID}" | tee -a "${LOGFILE}"
# API Call to DELETE a nodeid
curl -X DELETE "${ALFRESCO_HOST_URL}/alfresco/api/-default-/public/alfresco/versions/1/nodes/${DELETE_NODE_ID}?permanent=${DELETE_PERMANENT}" \
-H "accept: application/json" \
-H "authorization: Basic ${AUTH_PASS}"
}
saveChildrenNodeIDsToOutputfile
if [[ -z ${CHILDREN_NODE_IDS} ]]
then
echo "$(date +"%Y-%m-%d %H:%M:%S,%3N") - No Childen Node IDs found en Lost & Found Folder found" | tee -a "${LOGFILE}"
echo " " | tee -a "${LOGFILE}"
exit 0
else
echo "$(date +"%Y-%m-%d %H:%M:%S,%3N") - Amount of Lost & Found Children to process" | tee -a "${LOGFILE}"
echo "${CHILDREN_NODE_IDS}" | wc -l | tee -a "${LOGFILE}"
echo "" | tee -a "${LOGFILE}"
echo "$(date +"%Y-%m-%d %H:%M:%S,%3N") - Start Deletion of these children nodeids:" | tee -a "${LOGFILE}"
echo "" | tee -a "${LOGFILE}"
# Loop each line from CHILDREN_NODE_IDS
while IFS= read -r NODE_ID; do
deleteNodeID ${NODE_ID}
sleep ${WAIT_TIME}
done <<< "${CHILDREN_NODE_IDS}"
fi
echo ""
exit 0
@aitseitz
Copy link
Author

aitseitz commented Feb 2, 2024

Cleanup Alfresco's in lost&found folder from orphan child nodes

Script Information

This cleanup_alfresco_lost_and_found.sh script was created to delete nodes underneath the lost&found folder

image

Script Options

Before running the script, adjust the admin credentials & specify the UUID of the lost&found folder with

LOST_N_FOUND_FOLDER_NODE_ID=2f849cd8-9737-477d-9584-1af99a3b4b0e

The script iterates to all children underneath the lost& found folder and deletes them.
With the options

MAX_ITEMS=1000
WAIT_TIME=.2

you can decide how many childnodes should be deleted and in case of a production system if you want to add a delay with WAIT_TIME.


Historical Background of the Script

Due to an hardware failure (mount points to contentstore have not been available for some time)
we got the folloing errors in the log:

15:45:00,838 ERROR [org.alfresco.repo.domain.node.ibatis.NodeDAOImpl] ALF-13066: Orphan child node has been re-homed under lost_found: (6938228, workspace://SpacesStore/d0d237fa-8a3f-4316-8f5e-4e152978140a)
15:46:00,699 ERROR [org.alfresco.repo.domain.node.ibatis.NodeDAOImpl] ALF-13066: Orphan child node has been re-homed under lost_found: (6938237, workspace://SpacesStore/436dbf8a-464b-4e8c-a7e3-2f354365ed60)

Error-Message could be reproduced with a Testtransform from xlsx to pdf which produces an TestTransform.xls in the company home.
image
This Testtransform was called from an external monitoring system to check wether transformers are working correctly.
Due to the hardware failure, the deletion of that Testtransform.xlsx was still available underneath company home. Calling the Transformation a second time we've been able to reproduce the error message

10:28:10,079 ERROR [org.alfresco.repo.domain.node.ibatis.NodeDAOImpl] ALF-13066: Orphan child node has been re-homed under lost_found: (339833, workspace://SpacesStore/c1804dc9-dd86-4f90-856e-eeb9b651c1ee)

Quick solution was to delete the TestTransform.xls manually underneath company home folder.
Because it took some time to figure out what happend and therefore we endet up having lot nodes underneath the "lost&found" folder.

This Script helped us to clean up the orphan childnodes underneath the lost&found folder.

Additional Usefull Sql Queries:

SELECT * FROM alf_node n LEFT JOIN alf_child_assoc ca ON ca.child_node_id = n.id AND ca.is_primary=true WHERE ca.id is null
SELECT * FROM alf_node n WHERE n.id NOT IN (SELECT ca.child_node_id FROM alf_child_assoc ca WHERE ca.is_primary=true)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment