@@ -179,34 +179,38 @@ public function delete($deleteObjects = false)
179
179
180
180
public function deleteWithObjects ($ secondsToWait = null )
181
181
{
182
- // If timeout (seconds to wait) is not specified by caller, try to
183
- // estimate it based on number of objects in container
184
- if (null === $ secondsToWait ) {
185
- $ numObjects = (int ) $ this ->retrieveMetadata ()->getProperty ('Object-Count ' );
186
- $ secondsToWait = round ($ numObjects / 2 );
187
- }
188
-
189
- // Attempt to delete all objects and container
190
- $ endTime = time () + $ secondsToWait ;
191
- $ containerDeleted = false ;
192
- while ((time () < $ endTime ) && !$ containerDeleted ) {
193
- $ this ->deleteAllObjects ();
194
- try {
195
- $ response = $ this ->delete ();
196
- $ containerDeleted = true ;
197
- } catch (ContainerException $ e ) {
198
- // Ignore exception and try again
199
- } catch (ClientErrorResponseException $ e ) {
200
- if ($ e ->getResponse ()->getStatusCode () == 404 ) {
201
- // Container has been deleted
182
+ // If container is empty, just delete it
183
+ $ numObjects = (int ) $ this ->retrieveMetadata ()->getProperty ('Object-Count ' );
184
+ if ($ numObjects === 0 ) $ response = $ this ->delete ();
185
+
186
+ // Else attempt to delete all objects first, then delete container
187
+ else {
188
+ // If timeout (seconds to wait) is not specified by caller, try to
189
+ // estimate it based on number of objects in container
190
+ if (null === $ secondsToWait ) $ secondsToWait = round ($ numObjects / 2 );
191
+
192
+ // Attempt to delete all objects and container
193
+ $ endTime = time () + $ secondsToWait ;
194
+ $ containerDeleted = false ;
195
+ while ((time () < $ endTime ) && !$ containerDeleted ) {
196
+ $ this ->deleteAllObjects ();
197
+ try {
198
+ $ response = $ this ->delete ();
202
199
$ containerDeleted = true ;
203
- } else {
204
- throw $ e ;
200
+ } catch (ContainerException $ e ) {
201
+ // Ignore exception and try again
202
+ } catch (ClientErrorResponseException $ e ) {
203
+ if ($ e ->getResponse ()->getStatusCode () == 404 ) {
204
+ // Container has been deleted
205
+ $ containerDeleted = true ;
206
+ } else {
207
+ throw $ e ;
208
+ }
205
209
}
206
210
}
207
- }
208
- if (! $ containerDeleted ) {
209
- throw new ContainerException ( ' Container and all its objects cound not be deleted ' );
211
+ if (! $ containerDeleted ) {
212
+ throw new ContainerException ( ' Container and all its objects could not be deleted. ' );
213
+ }
210
214
}
211
215
return $ response ;
212
216
}
0 commit comments