public Bucket removeRetentionPolicy ( String bucketName ) throws StorageException , IllegalArgumentException { // [START storage_remove_retention_policy] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions . getDefaultInstance ( ) . getService ( ) ; // The name of a bucket, e.g. "my-bucket" // String bucketName = "my-bucket"; Bucket bucket = storage . get ( bucketName , BucketGetOption . fields ( BucketField . RETENTION_POLICY ) ) ; if ( bucket . retentionPolicyIsLocked ( ) != null && bucket . retentionPolicyIsLocked ( ) ) { throw new IllegalArgumentException ( "Unable to remove retention period as retention policy is locked." ) ; } Bucket bucketWithoutRetentionPolicy = bucket . toBuilder ( ) . setRetentionPeriod ( null ) . build ( ) . update ( ) ; System . out . println ( "Retention period for " + bucketName + " has been removed" ) ; // [END storage_remove_retention_policy] return bucketWithoutRetentionPolicy ; }
Example of removing a retention policy on a bucket