DeletingOneObjectUsingJava
Deleting an Object Using the AWS SDK for Java
You can delete an object from a bucket. If you have versioning enabled on the bucket, you have the following options:
- Delete a specific object version by specifying a version ID.
- Delete an object without specifying a version ID, in which case S3 adds a delete marker to the object.
For more information about versioning, see Object Versioning.
Example Example 1: Deleting an Object (Non-Versioned Bucket)
The following example deletes an object from a bucket. The example assumes that the bucket is not versioning-enabled and the object doesn't have any version IDs. In the delete request, you specify only the object key and not a version ID. For instructions on creating and testing a working sample, see Testing the Amazon S3 Java Code Examples.
Example Example 2: Deleting an Object (Versioned Bucket)
The following example deletes an object from a versioned bucket. The example deletes a specific object version by specifying the object key name and version ID. The example does the following:
Adds a sample object to the bucket. Amazon S3 returns the version ID of the newly added object. The example uses this version ID in the delete request.
Deletes the object version by specifying both the object key name and a version ID. If there are no other versions of that object, Amazon S3 deletes the object entirely. Otherwise, Amazon S3 only deletes the specified version. Note
You can get the version IDs of an object by sending aListVersions
request.