Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.storage.control.v2;

// [START storage_control_delete_folder_recursive]

import com.google.api.gax.longrunning.OperationFuture;
import com.google.protobuf.Empty;
import com.google.storage.control.v2.DeleteFolderRecursiveMetadata;
import com.google.storage.control.v2.DeleteFolderRecursiveRequest;
import com.google.storage.control.v2.FolderName;
import com.google.storage.control.v2.StorageControlClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public final class DeleteFolderRecursive {

public static void deleteFolderRecursive(String bucketName, String folderName)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// The name of the bucket
// String bucketName = "your-unique-bucket-name";

// The name of the folder within the bucket
// String folderName = "your-unique-folder-name";

try (StorageControlClient storageControl = StorageControlClient.create()) {

// Set project to "_" to signify globally scoped bucket
String folderResourceName = FolderName.format("_", bucketName, folderName);
DeleteFolderRecursiveRequest request =
DeleteFolderRecursiveRequest.newBuilder().setName(folderResourceName).build();

OperationFuture<Empty, DeleteFolderRecursiveMetadata> operationFuture =
storageControl.deleteFolderRecursiveAsync(request);

operationFuture.get(30, TimeUnit.SECONDS);

System.out.printf("Deleted folder: %s%n", folderResourceName);
}
}
}
// [END storage_control_delete_folder_recursive]
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.concurrent.TimeoutException;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;

Expand Down Expand Up @@ -139,6 +140,28 @@ public void deleteFolder() throws IOException {
assertThrows(NotFoundException.class, () -> storageControl.getFolder(folderName));
}

@Ignore("Feature not yet enabled for all buckets/projects")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this, the feature is allowlisted for the project running this test in CI

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Co-authored by AI Agent

@Test
public void deleteFolderRecursive()
throws IOException, ExecutionException, InterruptedException, TimeoutException {
String parentFolderName = UUID.randomUUID().toString();
FolderName parentFolder = FolderName.of("_", bucket.getName(), parentFolderName);
storageControl.createFolder(
BucketName.of("_", bucket.getName()),
Folder.getDefaultInstance(),
parentFolder.getFolder());

String childFolderName = parentFolderName + "/" + UUID.randomUUID().toString();
FolderName childFolder = FolderName.of("_", bucket.getName(), childFolderName);
storageControl.createFolder(
BucketName.of("_", bucket.getName()), Folder.getDefaultInstance(), childFolder.getFolder());

DeleteFolderRecursive.deleteFolderRecursive(bucket.getName(), parentFolder.getFolder());
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(parentFolder.toString());
assertThrows(NotFoundException.class, () -> storageControl.getFolder(parentFolder));
assertThrows(NotFoundException.class, () -> storageControl.getFolder(childFolder));
}

@Test
public void listFolder() throws IOException {
FolderName folderName = FolderName.of("_", bucket.getName(), UUID.randomUUID().toString());
Expand Down
Loading