22

Is it possible to recover a file I deleted from my S3 bucket?

20

According to the Amazon S3 Documentation:

Once deleted, there is no method to restore or undelete an object.

| improve this answer | |
  • It's unfortunate S3 doesn't support snapshots. – churnd Mar 24 '10 at 11:40
  • 2
    To give hope to some people, nowadays you can undelete files if your bucket has versioning enabled. – jweyrich Jun 14 '17 at 15:37
  • how dammit....? – scytale Dec 4 '18 at 21:41
46

If you have versioning enable, yes you can! On a versioned bucket, a delete action on a file does not really delete it but it adds a version with a "Delete Marker". You can delete the delete marker with the AWS CLI:

aws s3api delete-object --bucket yourbucket-name --key "yourfile" --version-id id_of_the_delete_marker

You can get all the files in the bucket with

aws --output text s3api list-object-versions --bucket yourbucket-name > files.txt

If you want to undelete all the files in the bucket you can try:

echo '#!/bin/bash' > undeleteScript.sh && aws --output text s3api list-object-versions --bucket yourbucket-name | grep -E "^DELETEMARKERS" | awk '{FS = "[\t]+"; print "aws s3api delete-object --bucket yourbucket-name --key \42"$3"\42 --version-id "$5";"}' >> undeleteScript.sh && . undeleteScript.sh; rm -f undeleteScript.sh;
| improve this answer | |
  • I appreciate this answer! Worked flawlessly. – tacotuesday Jan 4 '16 at 22:11
  • dude! your undelete all files script just saved my life. thank you. – herostwist Mar 16 '16 at 11:39
  • 1
    For the script above you need to add a --output=text if you want it to work. He's assuming you are outputting your AWS command via a .txt file and not .JSON file. – Brad Allison Aug 9 '17 at 19:51
  • I've tried the script but it doesn't restore the object, it deletes it permanently! May it be becuase I'm not the owner of the bucket? – nicola Jul 30 '18 at 10:34
  • 1
    Why is this not the accepted answer? @vonhogen please reconsider your choice :) – Janaka Bandara Sep 18 '18 at 4:05
3

Just updating this question as I was looking for the answer:

You can add VERSIONING to S3 buckets now. This will cause S3 to keep versions of an object even after deletion. Full documentation: http://docs.amazonwebservices.com/AmazonS3/latest/dev/Versioning.html

| improve this answer | |
  • Is it possible to retrieve a deleted file over AWS Console? Can't find a way to do that. – Rafael Oliveira Jan 30 '14 at 22:25
0

You can now list object versions using the AWS console: http://docs.aws.amazon.com/AmazonS3/latest/UG/managing-objects-versioned-bucket.html

If you have the right permissions, then you could delete the delete marker (the latest version of a deleted object) and the result would be that the object is available again.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.