18
18
19
19
// [START storage_delete_file]
20
20
import com .google .cloud .storage .Blob ;
21
+ import com .google .cloud .storage .BlobId ;
21
22
import com .google .cloud .storage .Storage ;
22
23
import com .google .cloud .storage .StorageOptions ;
23
24
@@ -38,16 +39,19 @@ public static void deleteObject(String projectId, String bucketName, String obje
38
39
System .out .println ("The object " + objectName + " wasn't found in " + bucketName );
39
40
return ;
40
41
}
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 );
51
55
}
52
56
}
53
57
// [END storage_delete_file]
0 commit comments