Skip to content

Instantly share code, notes, and snippets.

@thecfguy
Last active September 10, 2021 12:56
Show Gist options
  • Save thecfguy/ad7e401499619039bf51c1628e5c9cba to your computer and use it in GitHub Desktop.
Save thecfguy/ad7e401499619039bf51c1628e5c9cba to your computer and use it in GitHub Desktop.
BUCKET_NAME=YOUR_BUCKKET_NAME
KEY_PREFIX="folder/to/restore"
# list all files having deleteMarker as latest version, you may use to confirm versionid.
aws s3api list-object-versions --bucket $BUCKET_NAME --prefix $KEY_PREFIX --query 'DeleteMarkers[?IsLatest==`true`]'
# this will actually restore all files by deleting delete marker from deleted files.
aws s3api list-object-versions --bucket $BUCKET_NAME --prefix $KEY_PREFIX --output json --query 'DeleteMarkers[?IsLatest==`true`].[Key, VersionId]' | jq -r '.[] | "--key '\''" + .[0] + "'\'' --version-id " + .[1]' | xargs -L1 aws s3api delete-object --bucket $BUCKET_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment