|
| 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]) |
0 commit comments