Skip to content

Instantly share code, notes, and snippets.

@dcjohnson24
Last active May 9, 2022 20:20
Show Gist options
  • Save dcjohnson24/84dc554723185adbf05afb693bf23cd0 to your computer and use it in GitHub Desktop.
Save dcjohnson24/84dc554723185adbf05afb693bf23cd0 to your computer and use it in GitHub Desktop.
Delete multiple file systems on AWS EFS

Required packages

  • jq
  • pv
  • docker or AWS CLI

If you have the AWS CLI already installed, change aws_fetch to

aws_fetch () {
    aws efs describe-file-systems --query 'FileSystems[*].FileSystemId' --no-paginate \
        --no-cli-pager | jq . > file-system.json
}

and aws_delete to

aws_delete () {
    aws efs delete-file-system --no-cli-pager --file-system-id $1
}

AWS_DEFAULT_OUTPUT is set to json and AWS_PAGER is set to "".

#!/bin/bash
USER=$(whoami)
aws_delete () {
docker run --rm -i -e AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY -e AWS_DEFAULT_REGION \
-e AWS_DEFAULT_OUTPUT -e AWS_PAGER \
-v /home/${USER}/.aws:/root/.aws:z amazon/aws-cli:2.6.1 \
efs delete-file-system --no-cli-pager --file-system-id $1
}
aws_fetch () {
docker run --rm -i -e AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY -e AWS_DEFAULT_REGION \
-e AWS_DEFAULT_OUTPUT -e AWS_PAGER \
-v /home/${USER}/.aws:/root/.aws:z amazon/aws-cli:2.6.1 \
efs describe-file-systems --query 'FileSystems[*].FileSystemId' --no-paginate \
--no-cli-pager | jq . > file-system.json
}
if [ ! -f file-system.json ]
then
echo "Fetching the file systems to be removed"
aws_fetch
fi
total=$(jq ". | length" file-system.json)
echo "There are $total file systems to be removed"
if (( $total > 0 ))
then
readarray -t fs_array < <(jq -c -r ".[]" file-system.json)
for fsid in "${fs_array[@]}"; do
aws_delete $fsid
done | pv -p -s $total --line-mode --eta --timer > /dev/null
fi
echo "All done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment