-
Notifications
You must be signed in to change notification settings - Fork 6.7k
feat(storage): add deleteFolderRecursive sample #14305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
e4be53a
feat(python): add deleteFolderRecursive sample
nidhiii-27 54689cb
fix: resolve PR feedback for imports
nidhiii-27 7748fae
fix: re-enable Kokoro presubmit configs to fix CI failures
nidhiii-27 e025f93
fix: remove disabled Kokoro configs (re-enabled)
nidhiii-27 27eb583
fix: remove disabled Kokoro configs (re-enabled) - python3.8
nidhiii-27 72c20fe
fix: remove disabled Kokoro configs (re-enabled) - python3.12
nidhiii-27 27ec3ea
fix: remove remaining disabled Kokoro configs
nidhiii-27 65c475f
fix: remove remaining disabled Kokoro configs - 3.10
nidhiii-27 7f556fe
fix: remove remaining disabled Kokoro configs - 3.11
nidhiii-27 f4fe0c9
fix: remove remaining disabled Kokoro configs - 3.13
nidhiii-27 81b99af
fix: remove remaining disabled Kokoro configs - 3.14
nidhiii-27 1f15f41
fix: revert re-enabling of Kokoro jobs to resolve CI failures
nidhiii-27 6bbcfd0
fix: remove re-enabled Kokoro jobs
nidhiii-27 fea7d00
fix: remove re-enabled Kokoro jobs
nidhiii-27 f1603f8
fix: remove re-enabled Kokoro jobs
nidhiii-27 80d6ee7
fix: remove re-enabled Kokoro jobs
nidhiii-27 f37b4ae
fix: remove re-enabled Kokoro jobs
nidhiii-27 7ecaa94
fix: remove re-enabled Kokoro jobs
nidhiii-27 19cc3c8
fix: remove re-enabled Kokoro jobs
nidhiii-27 6b33ea9
fix: remove re-enabled Kokoro jobs
nidhiii-27 04f4b83
fix: re-enable Kokoro lint config to fix 'file not found' CI failure
nidhiii-27 3869fe1
fix: re-enable Kokoro python presubmit configs to fix 'file not found…
nidhiii-27 23c79a2
fix: re-enable all Kokoro python presubmit configs and fix lint errors
nidhiii-27 970395b
fix: re-enable all Kokoro python presubmit configs and fix lint errors
nidhiii-27 0518d3c
fix: re-enable all Kokoro python presubmit configs and fix lint errors
nidhiii-27 f10525d
fix: re-enable all Kokoro python presubmit configs and fix lint errors
nidhiii-27 d17b7d8
fix: re-enable all Kokoro python presubmit configs and fix lint errors
nidhiii-27 44d62b5
fix: re-enable all Kokoro python presubmit configs and fix lint errors
nidhiii-27 1fd48e0
fix: re-enable all Kokoro python presubmit configs and fix lint errors
nidhiii-27 304832d
fix: re-enable all Kokoro python presubmit configs and fix lint errors
nidhiii-27 e6e592e
fix: re-enable all Kokoro python presubmit configs and fix lint errors
nidhiii-27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import sys | ||
|
|
||
| # [START storage_control_delete_folder_recursive] | ||
| from google.cloud import storage_control_v2 | ||
|
|
||
|
|
||
| def delete_folder_recursive(bucket_name: str, folder_name: str) -> None: | ||
| # The ID of your GCS bucket | ||
| # bucket_name = "your-unique-bucket-name" | ||
|
|
||
| # The name of the folder to be deleted recursively | ||
| # folder_name = "folder-name" | ||
|
|
||
| storage_control_client = storage_control_v2.StorageControlClient() | ||
| # The storage bucket path uses the global access pattern, in which the "_" | ||
| # denotes this bucket exists in the global namespace. | ||
| # Set project to "_" to signify globally scoped bucket | ||
| folder_path = storage_control_client.folder_path( | ||
| project="_", bucket=bucket_name, folder=folder_name | ||
| ) | ||
|
|
||
| request = storage_control_v2.DeleteFolderRecursiveRequest( | ||
| name=folder_path, | ||
| ) | ||
| operation = storage_control_client.delete_folder_recursive(request=request) | ||
| operation.result() | ||
|
|
||
| print(f"Deleted folder: {folder_path}") | ||
|
|
||
|
|
||
| # [END storage_control_delete_folder_recursive] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| delete_folder_recursive(bucket_name=sys.argv[1], folder_name=sys.argv[2]) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if user doesn't provide 2nd argument ? (which might happen due to many reasons) ,
What is the behavior of
storage_control_client.delete_folder_recursiveUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Firstly, if user doesn't provide the folder_name, the program will throw an Index error.
Regarding the behavior of
storage_control_client.delete_folder_recursive: the folder resource path is constructed asprojects/_/buckets/{bucket_name}/folders/{folder_name}. If the folder name is empty, the API call fails with a validation error on the resource path and does not perform any deletion. If the bucket doesn't support hierarchical namespaces or if the folder does not exist, the API will fail with a404 Not Foundor400 Bad Requesterror.Co-authored by AI Agent