delete_all_objects ( $bucket, $pcre )

Deletes all Amazon S3 objects inside the specified bucket.

Access

public

Parameters

Parameter

Type

Required

Description

$bucket

string

Required

The name of the bucket to use.

$pcre

string

Optional

A Perl-Compatible Regular Expression (PCRE) to filter the names against. The default value is PCRE_ALL.

Returns

Type

Description

boolean

A value of true means that all objects were successfully deleted. A value of false means that at least one object failed to delete.

Examples

Delete ALL objects within a bucket.

// Instantiate the class
$s3 = new AmazonS3();
$bucket = 'my-bucket' . strtolower($s3->key);

$response = $s3->delete_all_objects($bucket);

// Success?
var_dump($response);
Result:
bool(true)

Related Methods

See Also

Source

Method defined in services/s3.class.php | Toggle source view (42 lines) | View on GitHub

public function delete_all_objects($bucket, $pcre = self::PCRE_ALL)
{
    // Collect all matches
    $list = $this->get_object_list($bucket, array('pcre' => $pcre));

    // As long as we have at least one match...
    if (count($list) > 0)
    {
        $objects = array();

        foreach ($list as $object)
        {
            $objects[] = array('key' => $object);
        }

        $batch = new CFBatchRequest();
        $batch->use_credentials($this->credentials);

        foreach (array_chunk($objects, 1000) as $object_set)
        {
            $this->batch($batch)->delete_objects($bucket, array(
                'objects' => $object_set
            ));
        }

        $responses = $this->batch($batch)->send();
        $is_ok = true;

        foreach ($responses as $response)
        {
            if (!$response->isOK() || isset($response->body->Error))
            {
                $is_ok = false;
            }
        }

        return $is_ok;
    }

    // If there are no matches, return true
    return true;
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback