Skip to content

Commit 981a3cf

Browse files
mabsalehmabsaleh
andauthored
Add script to efficiently delete a large number of nested empty folders in an HNS bucket. (#13497)
* Add script to efficiently delete a large number of nested empty folders. --------- Co-authored-by: mabsaleh <[email protected]>
1 parent 72a6f96 commit 981a3cf

File tree

2 files changed

+434
-0
lines changed

2 files changed

+434
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# GCS HNS Folder Management Scripts
2+
3+
This directory contains scripts for managing folders in Google Cloud Storage
4+
(GCS) Hierarchical Namespace (HNS) enabled buckets.
5+
6+
## Scripts
7+
8+
### `delete_empty_folders.py`
9+
10+
This script recursively deletes empty folders within a specified GCS bucket and
11+
(optional) prefix.
12+
13+
**Features:**
14+
15+
* **Recursive Deletion:** Traverses and deletes nested empty folders.
16+
* **Depth-First Deletion:** Deletes the deepest folders first to ensure parent
17+
folders are empty before deletion attempts.
18+
* **Parallel Execution:** Uses a thread pool to delete folders concurrently
19+
for improved performance.
20+
* **Configurable:** Allows setting the target bucket, folder prefix, and
21+
number of workers.
22+
* **Error Handling:** Retries on transient errors and logs failures.
23+
* **Progress Reporting:** Periodically logs deletion statistics.
24+
25+
**Usage:**
26+
27+
1. **Authenticate:** `bash gcloud auth application-default login`
28+
2. **Configure:** Update the variables at the top of the script:
29+
* `BUCKET_NAME`: The name of your GCS HNS bucket.
30+
* `FOLDER_PREFIX`: (Optional) The prefix to limit deletion scope (e.g.,
31+
`archive/`). Leave empty to scan the whole bucket. Must end with `/` if
32+
specified.
33+
* `MAX_WORKERS`: Number of concurrent deletion threads.
34+
3. **Run:** `bash python3 delete_empty_folders.py`
35+
36+
**Note:** This script *only* deletes folders. Folders containing any objects
37+
will not be deleted, and a "Failed Precondition" warning will be logged.

0 commit comments

Comments
 (0)