8

My wish is very simple and basic. However, I can't find any working/clear answer for it :D

I want to know how to delete a file inside a folder in Google Cloud storage.

If let's say I have a bucket name of xxxx.appspot.com and I have a folder inside called images and I have an image inside this folder (E.g. 123.jpg) which I want to delete.

This is the code I wrote, however it didn't delete it:

String bucketName = "xxxx.appspot.com";
GcsService gcsService = GcsServiceFactory.createGcsService();                   
gcsService.delete(new GcsFilename(bucketName,"images/123.jpg"));

Did I miss anything?

Thanks in Advance!

2 Answers 2

8

You can find some code samples in the documentation. In short, what you want to achieve can be done in Java using a couple of lines of code, given that you have the right permissions:

BlobId b = BlobId.of(bucketName, blobName);
boolean deleted = storage.delete(b);
8
  • 1
    yeah I know, but what blob name should look like ? Dec 6, 2017 at 12:53
  • I think it should be something like: String blobName = "images/123.jpg"
    – dsesto
    Dec 6, 2017 at 14:03
  • when I don this it give me this Exception java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor; at com.google.api.gax.retrying.BasicRetryingFuture.<init>(BasicRetryingFuture.java:77) at com.google.api.gax.retrying.DirectRetryingExecutor.createFuture(DirectRetryingExecutor.java:75) at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:53) at com.google.cloud.storage.StorageImpl.delete(StorageImpl.java:384) at com.google.cloud.storage.StorageImpl.delete(StorageImpl.java:397) Dec 6, 2017 at 14:10
  • are you importing the right library? import com.google.cloud.storage.*
    – dsesto
    Dec 6, 2017 at 14:13
  • yup Im sure and here is my code snipped Storage storage = StorageOptions.getDefaultInstance().getService(); BlobId bId = BlobId.of("xxxx.appspot.com", "images/123.jpg"); storage.delete(bId) Dec 6, 2017 at 14:23
1

The correct answer as of 2022 is the following:

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

Example taken from this page

1
  • I'm getting same error Tamer was facing. no such method error.
    – Ajay Patil
    Feb 9, 2023 at 6:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.