1 # -*- coding: utf-8 -*-
2 # Copyright 2011 Google Inc. All Rights Reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Implementation of rb command for deleting cloud storage buckets."""
17 from __future__
import absolute_import
19 from gslib
.cloud_api
import NotEmptyException
20 from gslib
.command
import Command
21 from gslib
.command_argument
import CommandArgument
22 from gslib
.cs_api_map
import ApiSelector
23 from gslib
.exception
import CommandException
24 from gslib
.storage_url
import StorageUrlFromString
25 from gslib
.util
import NO_MAX
32 _DETAILED_HELP_TEXT
= ("""
38 The rb command deletes a bucket. Buckets must be empty before you can delete
41 Be certain you want to delete a bucket before you do so, as once it is
42 deleted the name becomes available and another user may create a bucket with
43 that name. (But see also "DOMAIN NAMED BUCKETS" under "gsutil help naming"
44 for help carving out parts of the bucket name space.)
48 -f Continues silently (without printing error messages) despite
49 errors when removing buckets. If some buckets couldn't be removed,
50 gsutil's exit status will be non-zero even if this flag is set.
54 class RbCommand(Command
):
55 """Implementation of gsutil rb command."""
57 # Command specification. See base class for documentation.
58 command_spec
= Command
.CreateCommandSpec(
60 command_name_aliases
=[
61 'deletebucket', 'removebucket', 'removebuckets', 'rmdir'],
62 usage_synopsis
=_SYNOPSIS
,
65 supported_sub_args
='f',
67 provider_url_ok
=False,
69 gs_api_support
=[ApiSelector
.XML
, ApiSelector
.JSON
],
70 gs_default_api
=ApiSelector
.JSON
,
72 CommandArgument
.MakeZeroOrMoreCloudBucketURLsArgument()
75 # Help specification. See help_provider.py for documentation.
76 help_spec
= Command
.HelpSpec(
79 'deletebucket', 'removebucket', 'removebuckets', 'rmdir'],
80 help_type
='command_help',
81 help_one_line_summary
='Remove buckets',
82 help_text
=_DETAILED_HELP_TEXT
,
83 subcommand_help_text
={},
87 """Command entry point for the rb command."""
88 self
.continue_on_error
= False
90 for o
, unused_a
in self
.sub_opts
:
92 self
.continue_on_error
= True
96 for url_str
in self
.args
:
97 wildcard_url
= StorageUrlFromString(url_str
)
98 if wildcard_url
.IsObject():
99 raise CommandException('"rb" command requires a provider or '
101 # Wrap WildcardIterator call in try/except so we can avoid printing errors
102 # with -f option if a non-existent URL listed, permission denial happens
103 # while listing, etc.
105 # Need to materialize iterator results into a list to catch exceptions.
106 # Since this is listing buckets this list shouldn't be too large to fit
108 blrs
= list(self
.WildcardIterator(url_str
).IterBuckets())
109 except: # pylint: disable=bare-except
111 if self
.continue_on_error
:
116 url
= blr
.storage_url
117 self
.logger
.info('Removing %s...', url
)
119 self
.gsutil_api
.DeleteBucket(url
.bucket_name
, provider
=url
.scheme
)
120 except NotEmptyException
as e
:
121 if self
.continue_on_error
:
123 elif 'VersionedBucketNotEmpty' in e
.reason
:
124 raise CommandException('Bucket is not empty. Note: this is a '
125 'versioned bucket, so to delete all '
126 'objects\nyou need to use:'
127 '\n\tgsutil rm -r %s' % url
)
130 except: # pylint: disable=bare-except
131 if not self
.continue_on_error
:
134 if not did_some_work
:
135 raise CommandException('No URLs matched')
136 return 1 if some_failed
else 0