Search code examples
google-cloud-platformgoogle-cloud-storagegoogle-bucket

Delete all objects from a directory in google storage bucket using cloud storage options


I'm using cloud storage options from google cloud to manage data in a GCP bucket. I've a directory under which I want to delete all the objects before I start writing new objects. sample directory: gs://bucket-name/directory1/subdirectory/

I'm able to delete a single object using the following code, but how do I get list of all the objects in a directory and delete all of them?

import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
public class DeleteObject {
       public static void deleteObject(String projectId, String bucketName, String objectName) {
       // The ID of your GCP project
       // String projectId = "your-project-id";

      // The ID of your GCS bucket
      // String bucketName = "your-unique-bucket-name";

     // The ID of your GCS object
     // String objectName = "your-object-name";

     Storage storage = 
     StorageOptions.newBuilder().setProjectId(projectId).build().getService();
     storage.delete(bucketName, objectName);

     System.out.println("Object " + objectName + " was deleted from " + bucketName);
     }
   }

Solution

  • Iterate over objects in the bucket with a prefix of the directory name.

    See the following snippet:

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Page<Blob> blobs =
    storage.list(
            bucketName,
            Storage.BlobListOption.prefix(directoryPrefix), // directoryPrefix is the sub directory. 
            Storage.BlobListOption.currentDirectory());
        
    for (Blob blob : blobs.iterateAll()) {
      blob.delete(Blob.BlobSourceOption.generationMatch());
    }
    

    References: