Skip to content

Commit f4b1b46

Browse files
authored
feat(storage/control): add delete folder recursive sample (#13642)
Adds a Java code sample demonstrating hierarchical namespace recursive folder delete. Fixes: b/530058464
1 parent 33629c1 commit f4b1b46

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
import java.util.concurrent.TimeUnit;
30+
import java.util.concurrent.TimeoutException;
31+
32+
public final class DeleteFolderRecursive {
33+
34+
public static void deleteFolderRecursive(String bucketName, String folderName)
35+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
36+
// The name of the bucket
37+
// String bucketName = "your-unique-bucket-name";
38+
39+
// The name of the folder within the bucket
40+
// String folderName = "your-unique-folder-name";
41+
42+
try (StorageControlClient storageControl = StorageControlClient.create()) {
43+
44+
// Set project to "_" to signify globally scoped bucket
45+
String folderResourceName = FolderName.format("_", bucketName, folderName);
46+
DeleteFolderRecursiveRequest request =
47+
DeleteFolderRecursiveRequest.newBuilder().setName(folderResourceName).build();
48+
49+
OperationFuture<Empty, DeleteFolderRecursiveMetadata> operationFuture =
50+
storageControl.deleteFolderRecursiveAsync(request);
51+
52+
operationFuture.get(30, TimeUnit.SECONDS);
53+
54+
System.out.printf("Deleted folder: %s%n", folderResourceName);
55+
}
56+
}
57+
}
58+
// [END storage_control_delete_folder_recursive]

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

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

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

0 commit comments

Comments
 (0)