This page describes how to use the Bucket Lock feature, including working with retention policies and permanently locking them on buckets.
Before you begin
Before you can use the Bucket Lock feature, make sure the steps in the following sections have been completed.
Get required roles
To get the permissions that you need to use Bucket Lock, ask your
administrator to grant you the Storage Admin (roles/storage.admin
) role on the
bucket. This predefined role contains the permissions required to use
Bucket Lock. To see the exact permissions required, expand the
Required permissions section:
Required permissions
storage.buckets.get
storage.buckets.list
- This permission is only required if you plan on using the Google Cloud console to perform the instructions on this page.
storage.buckets.update
You might also be able to get these permissions with custom roles.
For information about granting roles on buckets, see Use IAM with buckets.
Set a retention policy on a bucket
To add, modify, or remove a retention policy on a bucket:
Console
- In the Google Cloud console, go to the Cloud Storage Buckets page.
In the list of buckets, click the name of the bucket whose retention policy you want to change.
Select the Protection tab near the top of the page.
In the Retention policy section, set your retention policy:
If no retention policy currently applies to the bucket, click the add_box Set Retention Policy link. Choose a unit of time and a length of time for your retention period.
If a retention policy currently applies to a bucket, it appears in the section. Click Edit to modify the retention time or Delete to remove the retention policy entirely.
See Retention periods for information about how the Google Cloud console converts between different units of time.
To learn how to get detailed error information about failed Cloud Storage operations in the Google Cloud console, see Troubleshooting.
Command line
Use the gcloud storage buckets update
command with the
appropriate flag:
gcloud storage buckets update gs://BUCKET_NAME FLAG
Where:
BUCKET_NAME
is the name of the relevant bucket. For example,my-bucket
.FLAG
is the desired setting for the bucket's retention period. Use one of the following formats:--retention-period
and a retention period, if you want to add or change a retention policy. For example,--retention-period=1d43200s
.--clear-retention-period
, if you want to remove the retention policy on the bucket.
If successful, the response looks like:
Updating gs://my-bucket/... Completed 1
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample sets a retention policy on a bucket: The following sample removes the retention policy from a bucket:
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample sets a retention policy on a bucket: The following sample removes the retention policy from a bucket:
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample sets a retention policy on a bucket: The following sample removes the retention policy from a bucket:
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample sets a retention policy on a bucket: The following sample removes the retention policy from a bucket:
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample sets a retention policy on a bucket: The following sample removes the retention policy from a bucket:
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample sets a retention policy on a bucket: The following sample removes the retention policy from a bucket:
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample sets a retention policy on a bucket: The following sample removes the retention policy from a bucket:
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
The following sample sets a retention policy on a bucket: The following sample removes the retention policy from a bucket:C++
C#
Go
Java
Node.js
PHP
Python
Ruby
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Create a JSON file that contains the following information:
{ "retentionPolicy": { "retentionPeriod": "TIME_IN_SECONDS" } }
Where
TIME_IN_SECONDS
is the amount of time in seconds that objects in the bucket must be retained. For example,2678400
. See Retention periods for information about how different units of time are measured using seconds.To remove the retention policy from a bucket, use the following in the JSON file:
{ "retentionPolicy": null }
Use
cURL
to call the JSON API with aPATCH
Bucket request:curl -X PATCH --data-binary @JSON_FILE_NAME \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME?fields=retentionPolicy"
Where:
JSON_FILE_NAME
is the path for the JSON file that you created in Step 2.BUCKET_NAME
is the name of the relevant bucket. For example,my-bucket
.
XML API
The XML API cannot be used to set or remove a retention policy on an existing bucket. It can only be used to include a retention policy with a new bucket.
Lock a bucket
To lock a bucket and permanently restrict edits to the bucket's retention policy:
Console
- In the Google Cloud console, go to the Cloud Storage Buckets page.
In the list of buckets, click the name of the bucket that you want to lock the retention policy for.
Select the Protection tab near the top of the page.
In the Retention policy section, click the Lock button.
The Lock retention policy? dialog box appears.
Read the Permanent notice.
In the Bucket name text box, type in the name of your bucket.
Click Lock policy.
To learn how to get detailed error information about failed Cloud Storage operations in the Google Cloud console, see Troubleshooting.
Command line
Use the gcloud storage buckets update
command with the
--lock-retention-period
flag:
gcloud storage buckets update gs://BUCKET_NAME --lock-retention-period
Where BUCKET_NAME
is the name of the relevant
bucket. For example, my-bucket
.
If successful, the response looks similar to the following example:
Updating gs://my-bucket/... Completed 1
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the JSON API with aPOST
Bucket request:curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/lockRetentionPolicy?ifMetagenerationMatch=BUCKET_METAGENERATION_NUMBER"
Where:
BUCKET_NAME
is the name of the relevant bucket. For example,my-bucket
.BUCKET_METAGENERATION_NUMBER
is the metageneration value for the bucket. For example,8
. You can find the metageneration value for your bucket by calling the JSON API with aGET
Bucket request.
XML API
The XML API cannot be used to lock a bucket. Use one of the other Cloud Storage tools, such as the Google Cloud console, instead.
View a bucket's retention policy and lock status
To view what, if any, retention policy is set on a bucket and whether that retention policy is locked:
Console
- In the Google Cloud console, go to the Cloud Storage Buckets page.
Click the name of the bucket whose status you want to view.
If a bucket has a retention policy, the retention period is displayed in the Protection field for the bucket. If the retention policy is not locked, a lock icon appears next to the retention period in an unlocked state. If the retention policy is locked, a lock icon appears next to the retention period in a locked state.
Command line
Use the gcloud storage buckets describe
command with the
--format
flag:
gcloud storage buckets describe gs://BUCKET_NAME --format="default(retention_policy)"
Where BUCKET_NAME
is the name of the bucket
whose retention policy you want to view. For example, my-bucket
.
If successful and a retention policy exists for the bucket, the response is similar to the following:
retention_policy: effectiveTime: '2022-10-04T18:51:22.161000+00:00' retentionPeriod: '129600'
If successful and a retention policy does not exist for the bucket, the response is similar to the following:
null
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the JSON API with aGET
Bucket request that includes the desiredfields
:curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME?fields=retentionPolicy"
Where
BUCKET_NAME
is the name of the relevant bucket. For example,my-bucket
.If the bucket has a retention policy set on it, the response looks like the following example:
{ "retentionPolicy": { "retentionPeriod": "TIME_IN_SECONDS", "effectiveTime": "DATETIME", "isLocked": "BOOLEAN" }, }
XML API
The XML API cannot be used to view the retention policy on a bucket. Use one of the other Cloud Storage tools, such as the Google Cloud console, instead.
What's next
- Learn more about retention policies.
- Learn how to use object holds.
- Learn about Object Lifecycle Management, which can automatically delete files once they reach their retention period.