Sample code for 30+ languages & platforms
Java

S3 Delete File

See more Amazon S3 Examples

Demonstrates how to delete a remote file (object) on the Amazon S3 service.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat HTTP API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // Insert your access key here:
        http.setAwsAccessKey("AWS_ACCESS_KEY");

        // Insert your secret key here:
        http.setAwsSecretKey("AWS_SECRET_KEY");

        String bucketName = "chilkattest";
        String objectName = "starfish.jpg";

        http.setKeepResponseBody(true);

        try {
            http.s3DeleteObject(bucketName, objectName);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        if (http.getLastStatus() != 204) {
            System.out.println("Status code = " + http.getLastStatus());
            System.out.println(http.getLastResponseBody());
            System.out.println("Failed.");
            return;
        }

        // 204 is the success response status.
        // When successful, the response body will be empty.
        System.out.println("Status code = " + http.getLastStatus());
        System.out.println("Success. Object deleted.");
    }
}