Deleting an Object

Deleting an object removes the object key from your bucket and makes the object permanently irretrievable. Simply send an HTTP DELETE request to the URL that identifies the object you want to delete, including the required authentication information.

The following sample code snippets demonstrate deletion of an Amazon S3 object.

Java

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the name of your bucket and key-name with the name of the object to delete.

    Response response = conn.delete("[bucket-name]", "[key-name]", null);
    
    if (response.connection.getResponseCode() == 204) {
    
        // object was deleted
    
    } else {
    
        // something bad happened
    
    }

C#

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the name of your bucket and key-name with the name of the object to delete.

    try {
    
        Response response = conn.delete("[bucket-name]", "[key-name]");
    
        response.Close();
    
    } catch ( WebException ex ) {
    
        // Something bad happened
    
    }
    
    

Perl

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the name of your bucket and key-name with the name of the object to delete.

    $conn->delete("[bucket-name]", "[key-name]");
    
    
    
    if ($response->http_response->code == 204) {
    
        # object was deleted
    
    } else {
    
        # something bad happened
    
    }

PHP

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the name of your bucket and key-name with the name of the object to delete.

    $req =& new HTTP_Request("http://s3.amazonaws.com/[bucket-name]/[key-name]");
    
    $req->setMethod("DELETE");
    
    setAuthorizationHeader($req);
    
    $req->sendRequest();
    
    
    
    if ($req->getResponseCode() == 204) {
    
        // bucket was deleted
    
    } else {
    
        // something bad happened
    
    }

Ruby

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the name of your bucket and key-name with the name of the object to delete.

    conn.delete("[bucket-name]", "[key-name]");
    
    
    
    if response.http_response.status == 204
    
      # object was deleted
    
    else
    
      # something bad happened
    
    end

Python

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the name of your bucket and key-name with the name of the object to delete.

    conn.delete("[bucket-name]", "[key-name]");
    
    
    
    if response.http_response.status == 204:
    
        # object was deleted
    
    else:
    
        # something bad happened

HTTP

The following is an example of an HTTP request.

# delete object request



DELETE /[bucket-name]/[key-name] HTTP/1.0

Date: Wed, 08 Mar 2006 04:06:19 GMT

Authorization: AWS [aws-access-key-id]:[header-signature]

Host: s3.amazonaws.com



# delete object response



HTTP/1.1 204 No Content

x-amz-id-2: RvtfYQa0bFoavLma0eYJ1tgK17N/N6AWs6VQOUBSdzNTXwTL5W9BGIxt+D6gK+7I

x-amz-request-id: 6F7B7DB829785419

Date: Wed, 08 Mar 2006 04:06:19 GMT

Connection: keep-alive

Server: AmazonS3