Skip to content

Commit b6fafdc

Browse files
authored
Merge pull request #21 from sparkfabrik/864_empty_aws_bucket_via_consecutive_aws_api_calls
refs sparkfabrik-innovation-team/board#864: empty an aws bucket iteratively in chunks of 1000
2 parents 08876c7 + 43f3538 commit b6fafdc

File tree

1 file changed

+46
-22
lines changed

1 file changed

+46
-22
lines changed
Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,61 @@
1-
#!/bin/sh
1+
#!/usr/bin/env bash
2+
3+
# This script is used to empty an aws s3 bucket.
4+
#
5+
# We first get the list of objects versions paying attention to get also
6+
# the DeleteMarkers since our buckets are versioned and a bucket cannot be
7+
# considered empty unless also the deletion markers have been deleted.
8+
# We then iteratively go through this list, deleting 1000 items at a time
9+
# (since that is the maximum limit accepted by the aws s3api delete-objects command).
210

311
# Source functions library.
4-
. ${BASE}/functions
12+
# shellcheck disable=SC1091
13+
. "${BASE}/functions"
514

615
# Check for the required input
716
if [ -z "${BUCKET}" ]; then
8-
echo "You have to define the bucket name"
17+
echo "You have to define the bucket name."
918
exit 12
1019
fi
1120

12-
# All the required inputs are present! Do the job
13-
echo "All the required inputs are present. Go on with the real job."
21+
echo "All the required inputs are present."
1422

1523
echo "Start process of delete the file versions and the delete markers."
1624
format_string "Parameters:" "g"
1725
echo "$(format_string "Bucket:" "bold") ${BUCKET}"
1826

19-
echo "Get objects."
20-
ITEMS=$(aws s3api list-object-versions --bucket "${BUCKET}" --output json | jq '. | to_entries | [.[] | select(.key | match("^Versions|DeleteMarkers$")) | .value[] | {Key:.Key,VersionId:.VersionId}]')
21-
22-
if [ -z "${ITEMS}" ]; then
23-
echo "There is no items to delete."
27+
echo "Get objects list."
28+
ITEMS_TEMP_FILE=$(mktemp)
29+
aws s3api list-object-versions --bucket "${BUCKET}" --output json | jq '. | to_entries | [.[] | select(.key | match("^Versions|DeleteMarkers$")) | .value[] | {Key:.Key,VersionId:.VersionId}]' > "${ITEMS_TEMP_FILE}"
30+
if [ ! -s "${ITEMS_TEMP_FILE}" ]; then
31+
echo Bucket is already empty, bailing out.
2432
exit 0
2533
fi
26-
27-
echo "Delete objects."
28-
aws s3api delete-objects --bucket "${BUCKET}" --delete "{\"Objects\": ${ITEMS}}"
29-
EXIT_CMD=$?
30-
31-
if [ ${EXIT_CMD} -ne 0 ]; then
32-
echo "Something went wrong during the objects delete."
33-
exit ${EXIT_CMD}
34-
fi
35-
36-
echo "Objects are deleted."
37-
exit ${EXIT_CMD}
34+
ITEMS_TOT_COUNT=$(jq 'length' < "${ITEMS_TEMP_FILE}")
35+
echo There are "$(format_string "${ITEMS_TOT_COUNT}" "bold")" items in the bucket.
36+
37+
PAGE_SIZE=1000
38+
I=0
39+
until (( I*PAGE_SIZE > ITEMS_TOT_COUNT )); do
40+
FROM=$(( I*PAGE_SIZE ))
41+
TO=$(( (I+1)*PAGE_SIZE ))
42+
ITEMS=$(jq ".[${FROM}:${TO}]" < "${ITEMS_TEMP_FILE}")
43+
COUNT_ITEMS=$(echo "${ITEMS}" | jq 'length')
44+
echo "Interval: [${FROM}:${TO}], deleting ${COUNT_ITEMS} objects."
45+
if (( COUNT_ITEMS == 0 )); then
46+
echo "No items left to delete."
47+
break
48+
fi
49+
aws s3api delete-objects --bucket "${BUCKET}" --delete "{\"Objects\": ${ITEMS}, \"Quiet\": true}"
50+
EXIT_CMD=$?
51+
if [ ${EXIT_CMD} -ne 0 ]; then
52+
echo "Something went wrong during objects deletion."
53+
exit ${EXIT_CMD}
54+
fi
55+
(( I+=1 ))
56+
done
57+
rm -f "${ITEMS_TEMP_FILE}"
58+
59+
60+
echo "Objects have been deleted."
61+
exit "${EXIT_CMD}"

0 commit comments

Comments
 (0)