0

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

1 Answer 1

3

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:

Sign up to request clarification or add additional context in comments.

3 Comments

This worked, but it also deleted the subdirectory. How do I only delete the objects and not the subdirectory?
@Dookoto_Sea folders does not exist in object storage systems like Cloud Storage. Please first check and understand how it works cloud.google.com/storage/docs/folders
Exactly, directories are not real. You said in your case that every time you add new objects, you need to do a clean up. so it won't be an issue if you uploaded objects with the required prefix again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.