|
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). |
2 | 10 |
|
3 | 11 | # Source functions library. |
4 | | -. ${BASE}/functions |
| 12 | +# shellcheck disable=SC1091 |
| 13 | +. "${BASE}/functions" |
5 | 14 |
|
6 | 15 | # Check for the required input |
7 | 16 | if [ -z "${BUCKET}" ]; then |
8 | | - echo "You have to define the bucket name" |
| 17 | + echo "You have to define the bucket name." |
9 | 18 | exit 12 |
10 | 19 | fi |
11 | 20 |
|
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." |
14 | 22 |
|
15 | 23 | echo "Start process of delete the file versions and the delete markers." |
16 | 24 | format_string "Parameters:" "g" |
17 | 25 | echo "$(format_string "Bucket:" "bold") ${BUCKET}" |
18 | 26 |
|
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. |
24 | 32 | exit 0 |
25 | 33 | 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