@@ -159,9 +159,8 @@ public function getBytesQuota()
159
159
public function delete ($ deleteObjects = false )
160
160
{
161
161
if ($ deleteObjects === true ) {
162
- if (!$ this ->deleteAllObjects ()) {
163
- throw new ContainerException ('Could not delete all objects within container. Cannot delete container. ' );
164
- }
162
+ // Delegate to auxiliary method
163
+ return $ this ->deleteWithObjects ();
165
164
}
166
165
167
166
try {
@@ -178,6 +177,40 @@ public function delete($deleteObjects = false)
178
177
}
179
178
}
180
179
180
+ public function deleteWithObjects ($ secondsToWait = null )
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
202
+ $ containerDeleted = true ;
203
+ } else {
204
+ throw $ e ;
205
+ }
206
+ }
207
+ }
208
+ if (!$ containerDeleted ) {
209
+ throw new ContainerException ('Container and all its objects cound not be deleted ' );
210
+ }
211
+ return $ response ;
212
+ }
213
+
181
214
/**
182
215
* Deletes all objects that this container currently contains. Useful when doing operations (like a delete) that
183
216
* require an empty container first.
@@ -187,39 +220,11 @@ public function delete($deleteObjects = false)
187
220
public function deleteAllObjects ()
188
221
{
189
222
$ paths = array ();
190
-
191
223
$ objects = $ this ->objectList ();
192
-
193
224
foreach ($ objects as $ object ) {
194
225
$ paths [] = sprintf ('/%s/%s ' , $ this ->getName (), $ object ->getName ());
195
226
}
196
-
197
- $ this ->getService ()->batchDelete ($ paths );
198
-
199
- return $ this ->waitUntilEmpty ();
200
- }
201
-
202
- /**
203
- * This is a method that makes batch deletions more convenient. It continually
204
- * polls the resource, waiting for its state to change. If the loop exceeds the
205
- * provided timeout, it breaks and returns FALSE.
206
- *
207
- * @param int $secondsToWait The number of seconds to run the loop
208
- * @return bool
209
- */
210
- public function waitUntilEmpty ($ secondsToWait = 60 , $ interval = 1 )
211
- {
212
- $ endTime = time () + $ secondsToWait ;
213
-
214
- while (time () < $ endTime ) {
215
- if ((int ) $ this ->retrieveMetadata ()->getProperty ('Object-Count ' ) === 0 ) {
216
- return true ;
217
- }
218
-
219
- sleep ($ interval );
220
- }
221
-
222
- return false ;
227
+ return $ this ->getService ()->batchDelete ($ paths );
223
228
}
224
229
225
230
/**
0 commit comments