Skip to content

Commit 47dfb4f

Browse files
committed
samples(storagecontrol): add delete folder recursive sample
[Generated-by: AI]
1 parent b419024 commit 47dfb4f

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import sys
16+
17+
# [START storage_control_delete_folder_recursive]
18+
from google.cloud import storage_control_v2
19+
20+
21+
def delete_folder_recursive(bucket_name: str, folder_name: str) -> None:
22+
# The ID of your GCS bucket
23+
# bucket_name = "your-unique-bucket-name"
24+
25+
# The name of the folder to be deleted
26+
# folder_name = "folder-name"
27+
28+
storage_control_client = storage_control_v2.StorageControlClient()
29+
# The storage bucket path uses the global access pattern, in which the "_"
30+
# denotes this bucket exists in the global namespace.
31+
folder_path = storage_control_client.folder_path(
32+
project="_", bucket=bucket_name, folder=folder_name
33+
)
34+
35+
request = storage_control_v2.DeleteFolderRecursiveRequest(
36+
name=folder_path,
37+
)
38+
operation = storage_control_client.delete_folder_recursive(request=request)
39+
operation.result()
40+
41+
print(f"Recursively deleted folder {folder_name}")
42+
43+
44+
# [END storage_control_delete_folder_recursive]
45+
46+
47+
if __name__ == "__main__":
48+
delete_folder_recursive(bucket_name=sys.argv[1], folder_name=sys.argv[2])
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.cloud import storage
16+
17+
import pytest
18+
19+
import create_folder
20+
import delete_folder_recursive
21+
22+
23+
def test_delete_folder_recursive(
24+
capsys: pytest.LogCaptureFixture, hns_enabled_bucket: storage.Bucket, uuid_name: str
25+
) -> None:
26+
bucket_name = hns_enabled_bucket.name
27+
folder_name = uuid_name
28+
29+
# Create a folder
30+
create_folder.create_folder(bucket_name=bucket_name, folder_name=folder_name)
31+
32+
# Create an object inside the folder so that recursive delete is required
33+
blob = hns_enabled_bucket.blob(f"{folder_name}/test.txt")
34+
blob.upload_from_string("test data")
35+
36+
# Delete folder recursively
37+
delete_folder_recursive.delete_folder_recursive(
38+
bucket_name=bucket_name, folder_name=folder_name
39+
)
40+
41+
out, _ = capsys.readouterr()
42+
assert folder_name in out
43+
44+
# Verify the object is deleted
45+
assert not blob.exists()

0 commit comments

Comments
 (0)