Skip to content

Commit 3f09199

Browse files
committed
feat(java): add deleteFolderRecursive sample
This adds a sample demonstrating how to recursively delete a folder in a hierarchical namespace bucket. Fixes: b/521168740 [Generated-by: AI]
1 parent 058efbb commit 3f09199

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.control.v2;
18+
19+
// [START storage_control_delete_folder_recursive]
20+
21+
import com.google.storage.control.v2.DeleteFolderRecursiveRequest;
22+
import com.google.storage.control.v2.FolderName;
23+
import com.google.storage.control.v2.StorageControlClient;
24+
import java.util.concurrent.ExecutionException;
25+
26+
public final class DeleteFolderRecursive {
27+
28+
public static void deleteFolderRecursive(String bucketName, String folderName)
29+
throws ExecutionException, InterruptedException, Exception {
30+
// The name of the bucket
31+
// String bucketName = "your-unique-bucket-name";
32+
33+
// The name of the folder within the bucket
34+
// String folderName = "your-unique-folder-name";
35+
36+
try (StorageControlClient storageControl = StorageControlClient.create()) {
37+
38+
// Set project to "_" to signify globally scoped bucket
39+
String folderResourceName = FolderName.format("_", bucketName, folderName);
40+
DeleteFolderRecursiveRequest request =
41+
DeleteFolderRecursiveRequest.newBuilder().setName(folderResourceName).build();
42+
43+
storageControl.deleteFolderRecursiveAsync(request).get();
44+
45+
System.out.printf("Deleted folder recursively: %s%n", folderResourceName);
46+
}
47+
}
48+
}
49+
// [END storage_control_delete_folder_recursive]

java-storage/samples/snippets/src/test/java/com/example/storage/control/v2/FoldersTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@ public void deleteFolder() throws IOException {
139139
assertThrows(NotFoundException.class, () -> storageControl.getFolder(folderName));
140140
}
141141

142+
@Test
143+
public void deleteFolderRecursive() throws Exception {
144+
FolderName folderName = FolderName.of("_", bucket.getName(), UUID.randomUUID().toString());
145+
Folder gen1 =
146+
storageControl.createFolder(
147+
BucketName.of("_", bucket.getName()),
148+
Folder.getDefaultInstance(),
149+
folderName.getFolder());
150+
151+
DeleteFolderRecursive.deleteFolderRecursive(bucket.getName(), folderName.getFolder());
152+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(folderName.toString());
153+
assertThrows(NotFoundException.class, () -> storageControl.getFolder(folderName));
154+
}
155+
142156
@Test
143157
public void listFolder() throws IOException {
144158
FolderName folderName = FolderName.of("_", bucket.getName(), UUID.randomUUID().toString());

0 commit comments

Comments
 (0)