'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);
     }
   }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source