Python Library for Deleting Objects in Google Cloud Storage Buckets
This library provides a function clean_up_gcs_bucket that helps you delete unwanted objects from a Google Cloud Storage (GCS) bucket based on a folder path and substring criteria.
** clean_up_gcs_bucket(bucket_name, folder_path, substring): **
The clean_up_gcs_bucket function takes the following arguments:
Parameters:
- bucket_name: (str) The name of the GCS bucket to clean up.
- folder_path (str, optional): An optional prefix to filter objects within a specific folder path in the bucket. Include a trailing slash. Defaults to listing all objects in the bucket.
- substring (str, optional): An optional substring to filter objects. Objects containing the substring (case-insensitive) will be deleted. Defaults to deleting all objects matching the folder_path criteria.
Example Usage:
from Clean_up_gcs_bucket import clean_up_gcs_bucket
# Replace with your information
bucket_name = "your-bucket-name"
folder_path = "path/to/your/folder/" # Optional, defaults to all files in bucket
substring = "report" # Optional, deletes objects containing the substring (case-insensitive)
clean_up_gcs_bucket(bucket_name, folder_path, substring)
Function Behavior
- Connects to GCS using storage.Client.
- Retrieves the bucket specified by bucket_name.
- Lists all blobs (objects) in the bucket, optionally filtered by folder_path.
- Iterates through each file in the bucket: a). Checks if the substring is provided: 1. If not provided, all objects matching the folder_path (if specified) will be deleted. 2. If provided, objects containing the substring (case-insensitive) and matching the folder_path (if specified) will be deleted. b). Deletes the matching blob using blob.delete(). c). Keeps a count of deleted objects.
- Prints a summary message indicating the number of deleted objects, bucket name, folder path (if provided), and the used substring (if provided).
Additional Notes:
- This function permanently deletes objects from GCS. Use caution and ensure you have backups if necessary.
- Be specific with folder_path and substring to avoid unintended deletions.
- This library uses the google-cloud-storage library. Ensure it is installed (pip install google-cloud-storage).
Metadata
Release files for Clean-up-gcs-bucket 0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| Clean_up_gcs_bucket-0.2.tar.gz | 2.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| Clean_up_gcs_bucket-0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 5.4 kB
Release files / Clean_up_gcs_bucket-0.2.tar.gz
| Download URL | Clean_up_gcs_bucket-0.2.tar.gz |
|---|---|
| Size | 2.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
828830b01595c260900e4a4a8f176481cfd16f8373d0cce62e764d52f0de487d
|
|
BLAKE2b-256 checksum How to use checksums |
27c724a0f9ea7b58ae3af466b58e273ba2cfae73490bcaed301dd957d2ce3fb8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / Clean_up_gcs_bucket-0.2-py3-none-any.whl
| Download URL | Clean_up_gcs_bucket-0.2-py3-none-any.whl |
|---|---|
| Size | 3.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e59f8f49670ca34c94e2f8b571108946265cbfa340224df45bbca22bdfe8479c
|
|
BLAKE2b-256 checksum How to use checksums |
bed7c4d3280d0c2cb2750da80bb3db55734bc4c0d2bb66c030e1b5e7e61393c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|