Skip to content

Commit 2c01d55

Browse files
authored
feat(storage/control): add delete folder recursive sample (#14397)
Re-applies the changes from #13642 (which was reverted in #14391 due to #14387) and includes librarian generation updates so that the librarian generate diff check succeeds. [Generated-by: AI]
1 parent a4a47da commit 2c01d55

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎java-storage/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/google-cloud-java/
310310
| Create Folder | [source code](https://github.com/googleapis/google-cloud-java/blob/main/samples/snippets/src/main/java/com/example/storage/control/v2/CreateFolder.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/google-cloud-java&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/control/v2/CreateFolder.java) |
311311
| Create Hierarchical Namespace Bucket | [source code](https://github.com/googleapis/google-cloud-java/blob/main/samples/snippets/src/main/java/com/example/storage/control/v2/CreateHierarchicalNamespaceBucket.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/google-cloud-java&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/control/v2/CreateHierarchicalNamespaceBucket.java) |
312312
| Delete Folder | [source code](https://github.com/googleapis/google-cloud-java/blob/main/samples/snippets/src/main/java/com/example/storage/control/v2/DeleteFolder.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/google-cloud-java&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/control/v2/DeleteFolder.java) |
313+
| Delete Folder Recursive | [source code](https://github.com/googleapis/google-cloud-java/blob/main/samples/snippets/src/main/java/com/example/storage/control/v2/DeleteFolderRecursive.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/google-cloud-java&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/control/v2/DeleteFolderRecursive.java) |
313314
| Get Folder | [source code](https://github.com/googleapis/google-cloud-java/blob/main/samples/snippets/src/main/java/com/example/storage/control/v2/GetFolder.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/google-cloud-java&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/control/v2/GetFolder.java) |
314315
| List Folders | [source code](https://github.com/googleapis/google-cloud-java/blob/main/samples/snippets/src/main/java/com/example/storage/control/v2/ListFolders.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/google-cloud-java&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/control/v2/ListFolders.java) |
315316
| Rename Folder | [source code](https://github.com/googleapis/google-cloud-java/blob/main/samples/snippets/src/main/java/com/example/storage/control/v2/RenameFolder.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/google-cloud-java&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/control/v2/RenameFolder.java) |
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.api.gax.longrunning.OperationFuture;
22+
import com.google.protobuf.Empty;
23+
import com.google.storage.control.v2.DeleteFolderRecursiveMetadata;
24+
import com.google.storage.control.v2.DeleteFolderRecursiveRequest;
25+
import com.google.storage.control.v2.FolderName;
26+
import com.google.storage.control.v2.StorageControlClient;
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
public final class DeleteFolderRecursive {
31+
32+
public static void deleteFolderRecursive(String bucketName, String folderName)
33+
throws IOException, ExecutionException, InterruptedException {
34+
// The name of the bucket
35+
// String bucketName = "your-unique-bucket-name";
36+
37+
// The name of the folder within the bucket
38+
// String folderName = "your-unique-folder-name";
39+
40+
try (StorageControlClient storageControl = StorageControlClient.create()) {
41+
42+
// Set project to "_" to signify globally scoped bucket
43+
String folderResourceName = FolderName.format("_", bucketName, folderName);
44+
DeleteFolderRecursiveRequest request =
45+
DeleteFolderRecursiveRequest.newBuilder().setName(folderResourceName).build();
46+
47+
OperationFuture<Empty, DeleteFolderRecursiveMetadata> operationFuture =
48+
storageControl.deleteFolderRecursiveAsync(request);
49+
50+
operationFuture.get();
51+
52+
System.out.printf("Deleted folder: %s%n", folderResourceName);
53+
}
54+
}
55+
}
56+
// [END storage_control_delete_folder_recursive]

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

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

142+
@Test
143+
public void deleteFolderRecursive() throws IOException, ExecutionException, InterruptedException {
144+
String parentFolderName = UUID.randomUUID().toString();
145+
FolderName parentFolder = FolderName.of("_", bucket.getName(), parentFolderName);
146+
storageControl.createFolder(
147+
BucketName.of("_", bucket.getName()),
148+
Folder.getDefaultInstance(),
149+
parentFolder.getFolder());
150+
151+
String childFolderName = parentFolderName + "/" + UUID.randomUUID().toString();
152+
FolderName childFolder = FolderName.of("_", bucket.getName(), childFolderName);
153+
storageControl.createFolder(
154+
BucketName.of("_", bucket.getName()), Folder.getDefaultInstance(), childFolder.getFolder());
155+
156+
DeleteFolderRecursive.deleteFolderRecursive(bucket.getName(), parentFolder.getFolder());
157+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(parentFolder.toString());
158+
assertThrows(NotFoundException.class, () -> storageControl.getFolder(parentFolder));
159+
assertThrows(NotFoundException.class, () -> storageControl.getFolder(childFolder));
160+
}
161+
142162
@Test
143163
public void listFolder() throws IOException {
144164
FolderName folderName = FolderName.of("_", bucket.getName(), UUID.randomUUID().toString());

0 commit comments

Comments
 (0)