Bulk delete using CQL query
Example script delete-organizsations for bulk deleting organizsations selected by a CQL query.
#!/bin/sh
if [ -z "$1" ]
then
echo "First parameter must be non-empty CQL query"
exit 1
fi
OKAPIURL="${OKAPIURL:-https://folio-snapshot-okapi.dev.folio.org}"
TENANT="${TENANT:-diku}"
OKAPIUSERNAME="${OKAPIUSERNAME:-diku_admin}"
OKAPIPASSWORD="${OKAPIPASSWORD:-admin}"
LOGIN=$( jq -n -c --arg username "$OKAPIUSERNAME" --arg password "$OKAPIPASSWORD" '{"username": $username, "password": $password}' )
curl -w"\n\n" -sS -j -c /tmp/cookies.txt -H "X-Okapi-Tenant: $TENANT" -H "Content-type: application/json" -d "$LOGIN" "$OKAPIURL/authn/login-with-expiry"
while true
do
R=$( curl -w"\n%{http_code}\n" -sS -b /tmp/cookies.txt -H "X-Okapi-Tenant: $TENANT" -H "X-Okapi-URL: $OKAPIURL" --get --data-urlencode "query=$1" "$OKAPIURL/organizations-storage/organizations?limit=1" )
STATUS=$( echo "$R" | tail -n 1 )
if [ "$STATUS" != "200" ]
then
echo "GET failed:"
echo "$R"
break
fi
JSON=$( echo "$R" | head -n -1 )
echo "$JSON" | jq -c '.[] | .[0]?'
ID=$( echo "$JSON" | jq -r '.[] | .[0]?.id?' )
LENGTH=${#ID}
[ "$LENGTH" -eq 36 ] || break
R=$( curl -w"\n%{http_code}\n" -sS -b /tmp/cookies.txt -H "X-Okapi-Tenant: $TENANT" -H "X-Okapi-URL: $OKAPIURL" -XDELETE "$OKAPIURL/organizations-storage/organizations/$ID" )
STATUS=$( echo "$R" | tail -n 1 )
if [ "$STATUS" != "204" ]
then
echo "DELETE failed:"
echo "$R"
break
fi
doneExample invokation:
delete-organizsations 'name=="*rubbish*"'
Use CQL query cql.allRecords=1 to delete all records:
delete-organizsations 'cql.allRecords=1'