Skip to content

Instantly share code, notes, and snippets.

@rupertbg
Created February 7, 2018 07:17
Show Gist options
  • Save rupertbg/bfe57ff5c4afcf6e23f85904e7498474 to your computer and use it in GitHub Desktop.
Save rupertbg/bfe57ff5c4afcf6e23f85904e7498474 to your computer and use it in GitHub Desktop.
AWS CLI delete policies including all versions (requires jq)
#!/bin/bash
set -euxo pipefail;
policies=$(cat ./policies.txt); # A new-line separated list of policy ARNs
for policy in $policies; do
policiesVersions="$(aws iam list-policy-versions --policy-arn "$policy" | jq -r ".Versions[] | select(.IsDefaultVersion == false) | .VersionId")";
for pv in $policiesVersions; do
aws iam delete-policy-version --policy-arn "$policy" --version-id $pv;
done;
aws iam delete-policy --policy-arn "$policy";
done;
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment