Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Deleting an Object Using the AWS SDK for PHP

You can delete an object from a bucket. If you have versioning enabled on the bucket, you can also delete a specific version of an object.

The following tasks guide you through using the PHP classes to delete an object. For more information about versioning, see Using Versioning.

Deleting One Object (Non-Versioned Bucket)

1

Create an instance of the AmazonS3 class by providing your AWS credentials.

2

Execute the AmazonS3::delete_object() method. You must provide a bucket name and a key name as parameters.

If you have not enabled versioning on the bucket, the operation deletes the object. If you have enabled versioning, the operation adds a delete marker. For more information, see Deleting One Object Per Request.


The following PHP code sample demonstrates the preceding steps using the delete_object() method.

// Instantiate the class.
$s3 = new AmazonS3();
$response = $s3->delete_object($bucket, $keyname);

Deleting a Specific Version of an Object (Version-Enabled Bucket)

1

Create an instance of the AmazonS3 class by providing your AWS credentials.

2

Execute the AmazonS3::delete_object() method by providing a bucket name, an object key, and a version Id as parameters.

The method deletes the specific version of the object.


The following PHP code sample demonstrates the preceding steps.

// Instantiate the class.
$s3 = new AmazonS3();
$response = $s3->delete_object($bucket, $keyname, array (
        'versionId' =>$versionId));    		

Example 1: Deleting an Object (Non-Versioned Bucket)

The following PHP code example deletes an object from a bucket. It does not provide a version Id in the delete request. If you have not enabled versioning on the bucket, Amazon S3 deletes the object. If you have enabled versioning, Amazon S3 adds a delete marker and the object is not deleted. For information about how to create and test a working sample, see Using the AWS SDK for PHP.

<?php
require_once '../sdk.class.php';

$bucket = '*** Provide a Bucket Name ***';
$keyname = '*** Provide a Key Name ***';

// Instantiate the class.
$s3 = new AmazonS3();

// Delete the object.
$response = $s3->delete_object($bucket, $keyname);

// Success?
var_dump($response->isOK());
?>


Example 2: Deleting an Object (Versioned Bucket)

The following PHP code example deletes an object from a versioned-bucket. The delete_object() method specifies an object key name and a version ID and removes the specific object version from the bucket.

To test the sample, you must provide a bucket name. The code sample performs the following tasks:

  1. Enable versioning on the bucket.

  2. Add a sample object to the bucket. In response, Amazon S3 returns the version ID of the newly added object.

  3. Delete the sample object using the delete_object() method.

For information about how to create and test a working sample, see Using the AWS SDK for PHP.

<?php
require_once '../sdk.class.php';
$bucket = '***Provide Bucket Name***; 
$keyname = '***Provide Object Key Name****';

// Instantiate the class.
$s3 = new AmazonS3();

// Enable versioning on the bucket.
$response = $s3->enable_versioning($bucket);

// Add a sample object.
$versionId = create_object($keyname, $bucket, $s3);

// Delete the object.
$response = $s3->delete_object($bucket,$keyname, array(
        'versionId' => $versionId));
	
// Success?
var_dump($response->isOK());

// Create a sample object.
function create_object($keyname, $bucket, $s3)
{
	$keys = array();
	$content = "This is the content body!";
	$response = $s3->create_object($bucket, $keyname, array(
	    'body' => $content,
        'acl'  => AmazonS3::ACL_AUTH_READ,
	    'contentType' => 'text/plain'
	    )
	);
	return $response->header['x-amz-version-id'];
}
?>