How to Enable Versioning in Google Cloud Storage
To enable versioning on a Google Cloud Storage bucket, use the
gsutil versioning set on gs://BUCKET_NAME command or set the versioning configuration in the Cloud Console. This keeps old versions of objects when they are overwritten or deleted.Syntax
Use the gsutil command-line tool to enable versioning on a bucket with this syntax:
gsutil versioning set on gs://BUCKET_NAME— turns versioning on.gsutil versioning set off gs://BUCKET_NAME— turns versioning off.
Replace BUCKET_NAME with your actual bucket name.
bash
gsutil versioning set on gs://BUCKET_NAMEExample
This example shows how to enable versioning on a bucket named my-sample-bucket using the gsutil tool.
bash
gsutil versioning set on gs://my-sample-bucket # To verify versioning status: gsutil versioning get gs://my-sample-bucket
Output
Enabled: Enabled
Common Pitfalls
Common mistakes when enabling versioning include:
- Trying to enable versioning on a non-existent bucket.
- Not having the right permissions to change bucket settings.
- Expecting versioning to apply retroactively to existing objects (it only affects new changes).
Always verify the bucket exists and you have storage.buckets.update permission.
bash
## Wrong: Enabling versioning on a bucket that does not exist # gsutil versioning set on gs://nonexistent-bucket # Error: BucketNotFoundException ## Right: Create bucket first, then enable versioning # gsutil mb gs://new-bucket # gsutil versioning set on gs://new-bucket
Quick Reference
| Command | Description |
|---|---|
| gsutil versioning set on gs://BUCKET_NAME | Enable versioning on the bucket |
| gsutil versioning set off gs://BUCKET_NAME | Disable versioning on the bucket |
| gsutil versioning get gs://BUCKET_NAME | Check versioning status of the bucket |
Key Takeaways
Enable versioning on a bucket with 'gsutil versioning set on gs://BUCKET_NAME'.
Versioning keeps old object versions when overwritten or deleted.
You must have permission to update bucket settings to enable versioning.
Versioning affects only new changes after it is enabled, not existing objects.
Verify versioning status with 'gsutil versioning get gs://BUCKET_NAME'.