AWS S3 bucket versioning is a feature to keep track of different changes in a file. Without versioning enabled for the S3 bucket, if you change an existing file and upload to the S3 bucket again, the previous file gets deleted, and an updated new file is uploaded. In the future, if you need the previous file, you can not access it as it was replaced by the new updated file.
To keep track of the old files, you can enable S3 bucket versioning. With versioning enabled, when an existing file is updated on S3, S3 will save the updated file along with the previous file. You can access both versions of the file at any time.
There are three states of the S3 versioning feature.
- Versioning disabled (default)
- Versioning enabled
- Versioning suspended
Once enabled, S3 bucket versioning can not be disabled but can be suspended. To delete an S3 bucket with versioning enabled, first, you need to delete all the current and previous versions, then you can delete the S3 bucket.
This blog explains the detailed procedure for enabling the S3 bucket versioning and retrieving and deleting the previous versions of the objects stored in the S3 bucket.
Enabling S3 bucket versioning
First, log into the AWS management console and go to the S3 service.
Go to the S3 bucket you want to enable S3 versioning for.
From the page, go to the Properties tab.
Scroll down to the Bucket Versioning section and click on the Edit button to edit the versioning status of the S3 bucket. It will also show the current status of the S3 bucket versioning feature.
When you click on the Edit button, it will ask to enable the bucket versioning. Select the Enable option and click on the Save changes button to enable the bucket versioning.
After enabling the S3 bucket versioning, it will now display a small toggle show versions button in the S3 console. When enabled, it will display a version ID of all the objects. For objects that are uploaded to S3 before enabling the S3 bucket versioning, it will show a null version ID.
Now update the application.log file and click on the Upload button to upload it to the S3 bucket.
It will open a new web page, and you can upload a file or a directory by dragging or selecting using the file explorer. Click on the Add files button to select the file from your computer using file explorer.
After adding the file with the same name, click on the Upload button to upload the updated file to the S3 bucket.
Now click on the show versions button again to list all the versions of the S3 object. It will display a tree-like structure of the S3 objects with previous versions and their IDs.
Now upload a new file to the S3 bucket, and the newly uploaded object will get a version ID.
Now delete the newly uploaded file using the S3 console. First, disable the Show versions button, select the file, and click on the Delete button to delete the file from S3.
It will ask for confirmation before deleting the S3 object. Enter delete in the input field to confirm if you want to delete the S3 object and click on the Delete objects button.
After deleting the S3 object, now check all the objects available in the S3 console. It will only display the application.log file and the application.conf file will not be listed.
Now toggle the show versions button to list all the previous versions of S3 objects. Now it will also show the deleted file there with a delete marker on it.
Whenever an object is deleted from the S3 bucket with versioning enabled, it will disappear from the console. But the object is not actually deleted, S3 puts a delete marker on the object, and it can be listed when the show versions button is enabled.
To restore the deleted version, just delete the S3 object with a delete marker on it. Select the S3 object with the delete marker and click on the delete button to restore the previous version.
As you delete the delete marker, the deleted object will be listed in the S3 console even without enabling the show versions button. In other words, the previous version has been restored.
Deleting S3 object with previous versions
To delete an S3 object with its previous versions completely, delete the latest version of the S3 object from the console. It will put the delete marker on the object, and the S3 object will disappear from the console.
After deleting the latest version, now toggle the show versions button to list all the previous versions of the object. Select the previous versions from the console and click on the delete button to delete them completely.
Now select the delete marker and click on the delete button to delete the object completely.
Suspending S3 bucket versioning
After S3 versioning has been enabled, it can not be disabled, but you can suspend it. After suspending S3 bucket versioning, the previous S3 objects will remain the same, and their versions will retain, but newly added objects will have only a single version. In other words, the S3 bucket will stop creating versions of newly created S3 objects.
To suspend S3 versioning, go to the properties tab and scroll down to the Bucket Versioning section. It will show the versioning status of the S3 bucket and has an Edit button to edit the versioning status. Click on the Edit button to edit the versioning status.
It will show the options to suspend and enable the S3 bucket versioning. Select the suspend option and click on the save changes button to suspend versioning.
After suspending S3 bucket versioning, now upload a file and check its versions by enabling the show versions button from the console.
The newly uploaded file does not have any version ID as we have suspended bucket versioning, but the previously uploaded file still has a version ID as it was uploaded before suspending the S3 bucket versioning.
Conclusion
S3 bucket versioning is a feature provided by S3 to keep track of old versions of the objects stored in S3. It can also be used to recover data after you have deleted it from S3. After enabling S3 bucket versioning, it can not be disabled but can be suspended. This blog describes a step-by-step procedure to enable S3 bucket versioning, recovering deleted files, deleting files and their previous versions, and suspending S3 bucket versioning.