How can I retrieve an Amazon S3 object that was deleted in a versioning-enabled bucket?

Last updated: 2020-05-20

I want to retrieve an object that was deleted from my Amazon Simple Storage Service (Amazon S3) bucket that has versioning enabled. How can I do that?

Short Description

When you delete an object from a version-enabled bucket, Amazon S3 creates a delete marker for the object. The delete marker becomes the current version of the object, and the actual object becomes the previous version. With a delete marker, Amazon S3 responds to requests for the object as though the object was deleted. For example, if you send a GET request for the object, Amazon S3 returns an error.

You can retrieve an object that was deleted from a version-enabled bucket in one of these ways:

  • Download the previous version of the object: To download the previous version of the object (the actual object), you must have permissions to s3:GetObjectVersion.
  • Remove the delete marker: After you remove the delete marker, the actual object becomes the current version of the object. To remove the delete marker, you must have permissions to s3:DeleteObjectVersion. Additionally, you must remove the delete marker using the AWS account that owns or created the bucket.
    Note: If the bucket has MFA Delete-enabled, you must use the designated multi-factor authentication (MFA) to remove the delete marker.

Resolution

Download the previous version of the object using the Amazon S3 console

1.    Open the Amazon S3 console.

2.    From the list of buckets, open the bucket of the deleted object.

3.    Navigate to the folder of the deleted object.

4.    For Versions, choose Show.

5.    In the search bar, enter the name of the deleted object.

6.    Select the previous version of the object (the actual object rather than the delete marker), and then choose Download.

Download the previous version of the object using the AWS Command Line Interface (AWS CLI)

1.    Run the list-object-versions command on the bucket:

Tip: The following example command includes the --prefix option, which filters the results to the specified key name prefix. This can help reduce the number of results and save you time if your bucket contains a high volume of object versions.

aws s3api list-object-versions --bucket AWSDOC-EXAMPLE-BUCKET --prefix example.txt

2.    From the command output, copy the version ID of the previous version of the object (the actual object rather than the delete marker).

3.    Run the get-object command for the version ID that you copied in the previous step:

aws s3api get-object --bucket AWSDOC-EXAMPLE-BUCKET --key example.txt --version-id example.d6tjAKF1iObKbEnNQkIMPjj

Remove the delete marker using the Amazon S3 console

1.    Open the Amazon S3 console from the AWS account that owns or created the bucket of the deleted object.

2.    From the list of buckets, open the bucket of the deleted object.

3.    Navigate to the folder of the deleted object.

4.    For Versions, choose Show.

5.    In the search bar, enter the name of the deleted object.

6.    Select the delete marker of the object.

Warning: Review your selection carefully to be sure that it is the delete marker. If you delete an object version, it cannot be retrieved.

7.    Choose Actions, and then choose Delete.

8.    In the Delete objects dialog box, choose Delete.

Remove the delete marker using the AWS CLI

1.    Run the list-object-versions command with the following --query parameter:

Tip: The following example command includes the --prefix option, which filters the results to the specified key name prefix. This can help reduce the number of results and save you time if your bucket contains a large number of delete markers.

aws s3api list-object-versions --bucket AWSDOC-EXAMPLE-BUCKET --prefix example.txt --query 'DeleteMarkers[?IsLatest==`true`]'

2.    The command returns all objects in the bucket that have been deleted. From the command output, copy the version ID of the delete marker for the object that you want to retrieve.

Warning: Review your the version ID carefully to be sure that it is the version ID of the delete marker. If you delete an object version, it cannot be retrieved.

3.    Run the delete-object command for the version ID that you copied in the previous step:

aws s3api delete-object --bucket AWSDOC-EXAMPLE-BUCKET --key example.txt --version-id 'example.d6tjAKF1iObKbEnNQkIMPjj'

4.    After you remove the delete marker, the actual object is returned when you list the objects in the bucket. To verify this, you can run the ls command:

aws s3 ls s3://AWSDOC-EXAMPLE-BUCKET

Did this article help you?

Anything we could improve?


Need more help?