Remove retention policy

Shows an example of how to remove a retention policy on a bucket. The bucket retention policy must be unlocked to successfully remove a policy.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C++

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 client libraries .

  namespace 
  
 gcs 
  
 = 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 storage 
 ; 
 using 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 StatusOr 
 ; 
 []( 
 gcs 
 :: 
 Client 
  
 client 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 bucket_name 
 ) 
  
 { 
  
 StatusOr<gcs 
 :: 
 BucketMetadata 
>  
 original 
  
 = 
  
 client 
 . 
 GetBucketMetadata 
 ( 
 bucket_name 
 ); 
  
 if 
  
 ( 
 ! 
 original 
 ) 
  
 throw 
  
 std 
 :: 
 move 
 ( 
 original 
 ). 
 status 
 (); 
  
 StatusOr<gcs 
 :: 
 BucketMetadata 
>  
 patched 
  
 = 
  
 client 
 . 
 PatchBucket 
 ( 
  
 bucket_name 
 , 
  
 gcs 
 :: 
 BucketMetadataPatchBuilder 
 (). 
 ResetRetentionPolicy 
 (), 
  
 gcs 
 :: 
 IfMetagenerationMatch 
 ( 
 original 
 - 
> metageneration 
 ())); 
  
 if 
  
 ( 
 ! 
 patched 
 ) 
  
 throw 
  
 std 
 :: 
 move 
 ( 
 patched 
 ). 
 status 
 (); 
  
 if 
  
 ( 
 ! 
 patched 
 - 
> has_retention_policy 
 ()) 
  
 { 
  
 std 
 :: 
 cout 
 << 
 "The bucket " 
 << 
 patched 
 - 
> name 
 () 
 << 
 " does not have a retention policy set. 
 \n 
 " 
 ; 
  
 return 
 ; 
  
 } 
  
 std 
 :: 
 cout 
 << 
 "The bucket " 
 << 
 patched 
 - 
> name 
 () 
 << 
 " retention policy is set to " 
 << 
 patched 
 - 
> retention_policy 
 () 
 << 
 ". This is unexpected, maybe a concurrent change by another" 
 << 
 " application? 
 \n 
 " 
 ; 
 } 
 

C#

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 client libraries .

  using 
  
  Google.Cloud.Storage.V1 
 
 ; 
 using 
  
 System 
 ; 
 public 
  
 class 
  
 RemoveRetentionPolicySample 
 { 
  
 public 
  
 void 
  
 RemoveRetentionPolicy 
 ( 
 string 
  
 bucketName 
  
 = 
  
 "your-unique-bucket-name" 
 ) 
  
 { 
  
 var 
  
 storage 
  
 = 
  
  StorageClient 
 
 . 
  Create 
 
 (); 
  
 var 
  
 bucket 
  
 = 
  
 storage 
 . 
 GetBucket 
 ( 
 bucketName 
 ); 
  
 if 
  
 ( 
 bucket 
 . 
 RetentionPolicy 
  
 != 
  
 null 
 ) 
  
 { 
  
 bool 
  
 isLocked 
  
 = 
  
 bucket 
 . 
 RetentionPolicy 
 . 
 IsLocked 
  
 ?? 
  
 false 
 ; 
  
 if 
  
 ( 
 isLocked 
 ) 
  
 { 
  
 throw 
  
 new 
  
 Exception 
 ( 
 "Retention Policy is locked." 
 ); 
  
 } 
  
 bucket 
 . 
 RetentionPolicy 
 . 
 RetentionPeriod 
  
 = 
  
 null 
 ; 
  
 storage 
 . 
 UpdateBucket 
 ( 
 bucket 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 $"Retention period for {bucketName} has been removed." 
 ); 
  
 } 
  
 } 
 } 
 

Go

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 client libraries .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 "time" 
  
 "cloud.google.com/go/storage" 
 ) 
 // removeRetentionPolicy removes bucket retention policy. 
 func 
  
 removeRetentionPolicy 
 ( 
 w 
  
 io 
 . 
  Writer 
 
 , 
  
 bucketName 
  
 string 
 ) 
  
 error 
  
 { 
  
 // bucketName := "bucket-name" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 storage 
 . 
 NewClient 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "storage.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 ctx 
 , 
  
 cancel 
  
 := 
  
 context 
 . 
 WithTimeout 
 ( 
 ctx 
 , 
  
 time 
 . 
 Second 
 * 
 50 
 ) 
  
 defer 
  
 cancel 
 () 
  
 bucket 
  
 := 
  
 client 
 . 
  Bucket 
 
 ( 
 bucketName 
 ) 
  
 attrs 
 , 
  
 err 
  
 := 
  
 bucket 
 . 
 Attrs 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "Bucket(%q).Attrs: %w" 
 , 
  
 bucketName 
 , 
  
 err 
 ) 
  
 } 
  
 if 
  
 attrs 
 . 
  RetentionPolicy 
 
 . 
 IsLocked 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "retention policy is locked" 
 ) 
  
 } 
  
 bucketAttrsToUpdate 
  
 := 
  
 storage 
 . 
  BucketAttrsToUpdate 
 
 { 
  
 RetentionPolicy 
 : 
  
& storage 
 . 
  RetentionPolicy 
 
 {}, 
  
 } 
  
 if 
  
 _ 
 , 
  
 err 
  
 := 
  
 bucket 
 . 
 Update 
 ( 
 ctx 
 , 
  
 bucketAttrsToUpdate 
 ); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "Bucket(%q).Update: %w" 
 , 
  
 bucketName 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Retention period for %v has been removed\n" 
 , 
  
 bucketName 
 ) 
  
 return 
  
 nil 
 } 
 

Java

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 client libraries .

  import 
  
 com.google.cloud.storage. Bucket 
 
 ; 
 import 
  
 com.google.cloud.storage. Storage 
 
 ; 
 import 
  
 com.google.cloud.storage. StorageException 
 
 ; 
 import 
  
 com.google.cloud.storage. StorageOptions 
 
 ; 
 public 
  
 class 
 RemoveRetentionPolicy 
  
 { 
  
 public 
  
 static 
  
 void 
  
 removeRetentionPolicy 
 ( 
 String 
  
 projectId 
 , 
  
 String 
  
 bucketName 
 ) 
  
 throws 
  
  StorageException 
 
 , 
  
 IllegalArgumentException 
  
 { 
  
 // The ID of your GCP project 
  
 // String projectId = "your-project-id"; 
  
 // The ID of your GCS bucket 
  
 // String bucketName = "your-unique-bucket-name"; 
  
  Storage 
 
  
 storage 
  
 = 
  
  StorageOptions 
 
 . 
 newBuilder 
 (). 
 setProjectId 
 ( 
 projectId 
 ). 
 build 
 (). 
  getService 
 
 (); 
  
  Bucket 
 
  
 bucket 
  
 = 
  
 storage 
 . 
  get 
 
 ( 
  
 bucketName 
 , 
  
 Storage 
 . 
 BucketGetOption 
 . 
 fields 
 ( 
 Storage 
 . 
 BucketField 
 . 
 RETENTION_POLICY 
 )); 
  
 if 
  
 ( 
 bucket 
 . 
  retentionPolicyIsLocked 
 
 () 
  
 != 
  
 null 
 && 
 bucket 
 . 
  retentionPolicyIsLocked 
 
 ()) 
  
 { 
  
 throw 
  
 new 
  
 IllegalArgumentException 
 ( 
  
 "Unable to remove retention policy as retention policy is locked." 
 ); 
  
 } 
  
 bucket 
 . 
  toBuilder 
 
 (). 
 setRetentionPeriod 
 ( 
 null 
 ). 
 build 
 (). 
 update 
 (); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Retention policy for " 
  
 + 
  
 bucketName 
  
 + 
  
 " has been removed" 
 ); 
  
 } 
 } 
 

Node.js

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 client libraries .

  /** 
 * TODO(developer): Uncomment the following lines before running the sample. 
 */ 
 // The ID of your GCS bucket 
 // const bucketName = 'your-unique-bucket-name'; 
 // Imports the Google Cloud client library 
 const 
  
 { 
 Storage 
 } 
  
 = 
  
 require 
 ( 
 ' @google-cloud/storage 
' 
 ); 
 // Creates a client 
 const 
  
 storage 
  
 = 
  
 new 
  
 Storage 
 (); 
 async 
  
 function 
  
 removeRetentionPolicy 
 () 
  
 { 
  
 const 
  
 [ 
 metadata 
 ] 
  
 = 
  
 await 
  
 storage 
 . 
 bucket 
 ( 
 bucketName 
 ). 
 getMetadata 
 (); 
  
 if 
  
 ( 
 metadata 
 . 
  retentionPolicy 
 
 && 
 metadata 
 . 
  retentionPolicy 
 
 . 
 isLocked 
 ) 
  
 { 
  
 console 
 . 
 log 
 ( 
  
 'Unable to remove retention period as retention policy is locked.' 
  
 ); 
  
 return 
  
 null 
 ; 
  
 } 
  
 else 
  
 { 
  
 const 
  
 results 
  
 = 
  
 await 
  
 storage 
 . 
 bucket 
 ( 
 bucketName 
 ). 
  removeRetentionPeriod 
 
 (); 
  
 console 
 . 
 log 
 ( 
 `Removed bucket 
 ${ 
 bucketName 
 } 
 retention policy.` 
 ); 
  
 return 
  
 results 
 ; 
  
 } 
 } 
 removeRetentionPolicy 
 (). 
 catch 
 ( 
 console 
 . 
 error 
 ); 
 

PHP

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 client libraries .

  use Google\Cloud\Storage\StorageClient; 
 /** 
 * Removes a bucket's retention policy. 
 * 
 * @param string $bucketName The name of your Cloud Storage bucket. 
 *        (e.g. 'my-bucket') 
 */ 
 function remove_retention_policy(string $bucketName): void 
 { 
 $storage = new StorageClient(); 
 $bucket = $storage->bucket($bucketName); 
 $bucket->reload(); 
 if (array_key_exists('isLocked', $bucket->info()['retentionPolicy']) 
&& $bucket->info()['retentionPolicy']['isLocked']) { 
 printf('Unable to remove retention period as retention policy is locked.' . PHP_EOL); 
 return; 
 } 
 $bucket->update([ 
 'retentionPolicy' => [] 
 ]); 
 printf('Removed bucket %s retention policy' . PHP_EOL, $bucketName); 
 } 
 

Python

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 client libraries .

  from 
  
 google.cloud 
  
 import 
  storage 
 
 def 
  
 remove_retention_policy 
 ( 
 bucket_name 
 ): 
  
 """Removes the retention policy on a given bucket""" 
 # bucket_name = "my-bucket" 
 storage_client 
 = 
  storage 
 
 . 
  Client 
 
 () 
 bucket 
 = 
 storage_client 
 . 
  bucket 
 
 ( 
 bucket_name 
 ) 
 bucket 
 . 
 reload 
 () 
 if 
 bucket 
 . 
  retention_policy_locked 
 
 : 
 print 
 ( 
 "Unable to remove retention period as retention policy is locked." 
 ) 
 return 
 bucket 
 . 
  retention_period 
 
 = 
 None 
 bucket 
 . 
 patch 
 () 
 print 
 ( 
 f 
 "Removed bucket 
 { 
 bucket 
 . 
 name 
 } 
 retention policy" 
 ) 
 

Ruby

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 client libraries .

  def 
  
 remove_retention_policy 
  
 bucket_name 
 : 
  
 # The ID of your GCS bucket 
  
 # bucket_name = "your-unique-bucket-name" 
  
 require 
  
 "google/cloud/storage" 
  
 storage 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  Storage 
 
 . 
  new 
 
  
 bucket 
  
 = 
  
 storage 
 . 
 bucket 
  
 bucket_name 
  
 if 
  
 ! 
 bucket 
 . 
  retention_policy_locked? 
 
  
 bucket 
 . 
  retention_period 
 
  
 = 
  
 nil 
  
 puts 
  
 "Retention policy for 
 #{ 
 bucket_name 
 } 
 has been removed." 
  
 else 
  
 puts 
  
 "Policy is locked and retention policy can't be removed." 
  
 end 
 end 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Create a Mobile Website
View Site in Mobile | Classic
Share by: