1818
1919// [START storage_delete_file]
2020import com .google .cloud .storage .Blob ;
21+ import com .google .cloud .storage .BlobId ;
2122import com .google .cloud .storage .Storage ;
2223import com .google .cloud .storage .StorageOptions ;
2324
@@ -38,16 +39,19 @@ public static void deleteObject(String projectId, String bucketName, String obje
3839 System .out .println ("The object " + objectName + " wasn't found in " + bucketName );
3940 return ;
4041 }
41-
42- // Optional: set a generation-match precondition to avoid potential race
43- // conditions and data corruptions. The request to upload returns a 412 error if
44- // the object's generation number does not match your precondition.
45- Storage .BlobSourceOption precondition =
46- Storage .BlobSourceOption .generationMatch (blob .getGeneration ());
47-
48- storage .delete (bucketName , objectName , precondition );
49-
50- System .out .println ("Object " + objectName + " was deleted from " + bucketName );
42+ BlobId idWithGeneration = blob .getBlobId ();
43+ // Deletes the blob specified by its id. When the generation is present and non-null it will be
44+ // specified in the request.
45+ // If versioning is enabled on the bucket and the generation is present in the delete request,
46+ // only the version of the object with the matching generation will be deleted.
47+ // If instead you want to delete the current version, the generation should be dropped by
48+ // performing the following.
49+ // BlobId idWithoutGeneration =
50+ // BlobId.of(idWithGeneration.getBucket(), idWithGeneration.getName());
51+ // storage.delete(idWithoutGeneration);
52+ storage .delete (idWithGeneration );
53+
54+ System .out .println ("Object " + objectName + " was permanently deleted from " + bucketName );
5155 }
5256}
5357// [END storage_delete_file]
0 commit comments