Skip to content

Commit 0484464

Browse files
committed
Allow recursively deleting not-empty buckets
Closes #236 and #225 A much-requested feature of the obj plugin was to add the ability to delete buckets that aren't empty, or otherwise remove many objects in one operation. Unfortunately this has to happen client-side, so it can take a while for large buckets. This change adds a `--recursive` flag to `linode-cli obj rb` that will, if given, remove all objects from the bucket before removing the bucket. As implemented here this will not abort in-progress multi-part uploads or clear stale object versions; that may be added later if it's necessary (but if such things exist, they will prevent the bucket deletion from completing successfully).
1 parent ff44c83 commit 0484464

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

linodecli/plugins/obj.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,25 @@ def delete_bucket(get_client, args):
154154

155155
parser.add_argument('name', metavar='NAME', type=str,
156156
help="The name of the bucket to remove.")
157+
parser.add_argument('--recursive', action="store_true",
158+
help="If given, force removal of non-empty buckets by deleting "
159+
"all objects in the bucket before deleting the bucket. For "
160+
"large buckets, this may take a while.")
157161

158162
parsed = parser.parse_args(args)
159163
client = get_client()
160164

165+
if parsed.recursive:
166+
try:
167+
bucket = client.get_bucket(parsed.name)
168+
except S3ResponseError:
169+
print('No bucket named '+parsed.name)
170+
sys.exit(2)
171+
172+
for c in bucket.list():
173+
print("delete: {} {}".format(parsed.name, c.key))
174+
bucket.delete_key(c)
175+
161176
client.delete_bucket(parsed.name)
162177

163178
print("Bucket {} removed".format(parsed.name))

0 commit comments

Comments
 (0)