delete_objects
(**kwargs)¶This action enables you to delete multiple objects from a bucket using a single HTTP request. If you know the object keys that you want to delete, then this action provides a suitable alternative to sending individual delete requests, reducing per-request overhead.
The request contains a list of up to 1000 keys that you want to delete. In the XML, you provide the object key names, and optionally, version IDs if you want to delete a specific version of the object from a versioning-enabled bucket. For each key, Amazon S3 performs a delete action and returns the result of that delete, success, or failure, in the response. Note that if the object specified in the request is not found, Amazon S3 returns the result as deleted.
The action supports two modes for the response: verbose and quiet. By default, the action uses verbose mode in which the response includes the result of deletion of each key in your request. In quiet mode the response includes only keys where the delete action encountered an error. For a successful deletion, the action does not return any information about the delete in the response body.
When performing this action on an MFA Delete enabled bucket, that attempts to delete any versioned objects, you must include an MFA token. If you do not provide one, the entire request will fail, even if there are non-versioned objects you are trying to delete. If you provide an invalid token, whether there are versioned keys in the request or not, the entire Multi-Object Delete request will fail. For information about MFA Delete, see MFA Delete.
Finally, the Content-MD5 header is required for all Multi-Object Delete requests. Amazon S3 uses the header value to ensure that your request body has not been altered in transit.
The following operations are related to DeleteObjects
:
See also: AWS API Documentation
Request Syntax
response = client.delete_objects(
Bucket='string',
Delete={
'Objects': [
{
'Key': 'string',
'VersionId': 'string'
},
],
'Quiet': True|False
},
MFA='string',
RequestPayer='requester',
BypassGovernanceRetention=True|False,
ExpectedBucketOwner='string',
ChecksumAlgorithm='CRC32'|'CRC32C'|'SHA1'|'SHA256'
)
[REQUIRED]
The bucket name containing the objects to delete.
When using this action with an access point, you must direct requests to the access point hostname. The access point hostname takes the form AccessPointName -AccountId .s3-accesspoint.*Region* .amazonaws.com. When using this action with an access point through the Amazon Web Services SDKs, you provide the access point ARN in place of the bucket name. For more information about access point ARNs, see Using access points in the Amazon S3 User Guide .
When using this action with Amazon S3 on Outposts, you must direct requests to the S3 on Outposts hostname. The S3 on Outposts hostname takes the form AccessPointName-AccountId.outpostID.s3-outposts.Region.amazonaws.com
. When using this action with S3 on Outposts through the Amazon Web Services SDKs, you provide the Outposts bucket ARN in place of the bucket name. For more information about S3 on Outposts ARNs, see Using Amazon S3 on Outposts in the Amazon S3 User Guide .
[REQUIRED]
Container for the request.
The objects to delete.
Object Identifier is unique value to identify objects.
Key name of the object.
Warning
Replacement must be made for object keys containing special characters (such as carriage returns) when using XML requests. For more information, see XML related object key constraints.
VersionId for the specific version of the object to delete.
Element to enable quiet mode for the request. When you add this element, you must set its value to true.
s3:BypassGovernanceRetention
permission.403 Forbidden
(access denied).Indicates the algorithm used to create the checksum for the object when using the SDK. This header will not provide any additional functionality if not using the SDK. When sending this header, there must be a corresponding x-amz-checksum
or x-amz-trailer
header sent. Otherwise, Amazon S3 fails the request with the HTTP status code 400 Bad Request
. For more information, see Checking object integrity in the Amazon S3 User Guide .
If you provide an individual checksum, Amazon S3 ignores any provided ChecksumAlgorithm
parameter.
This checksum algorithm must be the same for all parts and it match the checksum value supplied in the CreateMultipartUpload
request.
dict
Response Syntax
{
'Deleted': [
{
'Key': 'string',
'VersionId': 'string',
'DeleteMarker': True|False,
'DeleteMarkerVersionId': 'string'
},
],
'RequestCharged': 'requester',
'Errors': [
{
'Key': 'string',
'VersionId': 'string',
'Code': 'string',
'Message': 'string'
},
]
}
Response Structure
(dict) --
Deleted (list) --
Container element for a successful delete. It identifies the object that was successfully deleted.
(dict) --
Information about the deleted object.
Key (string) --
The name of the deleted object.
VersionId (string) --
The version ID of the deleted object.
DeleteMarker (boolean) --
Specifies whether the versioned object that was permanently deleted was (true) or was not (false) a delete marker. In a simple DELETE, this header indicates whether (true) or not (false) a delete marker was created.
DeleteMarkerVersionId (string) --
The version ID of the delete marker created as a result of the DELETE operation. If you delete a specific object version, the value returned by this header is the version ID of the object version deleted.
RequestCharged (string) --
If present, indicates that the requester was successfully charged for the request.
Errors (list) --
Container for a failed delete action that describes the object that Amazon S3 attempted to delete and the error it encountered.
(dict) --
Container for all error elements.
Key (string) --
The error key.
VersionId (string) --
The version ID of the error.
Code (string) --
The error code is a string that uniquely identifies an error condition. It is meant to be read and understood by programs that detect and handle errors by type.
Amazon S3 error codes
AWS4-HMAC-SHA256
.Message (string) --
The error message contains a generic description of the error condition in English. It is intended for a human audience. Simple programs display the message directly to the end user if they encounter an error condition they don't know how or don't care to handle. Sophisticated programs with more exhaustive error handling and proper internationalization are more likely to ignore the error message.
Examples
The following example deletes objects from a bucket. The request specifies object versions. S3 deletes specific object versions and returns the key and versions of deleted objects in the response.
response = client.delete_objects(
Bucket='examplebucket',
Delete={
'Objects': [
{
'Key': 'HappyFace.jpg',
'VersionId': '2LWg7lQLnY41.maGB5Z6SWW.dcq0vx7b',
},
{
'Key': 'HappyFace.jpg',
'VersionId': 'yoz3HB.ZhCS_tKVEmIOr7qYyyAaZSKVd',
},
],
'Quiet': False,
},
)
print(response)
Expected Output:
{
'Deleted': [
{
'Key': 'HappyFace.jpg',
'VersionId': 'yoz3HB.ZhCS_tKVEmIOr7qYyyAaZSKVd',
},
{
'Key': 'HappyFace.jpg',
'VersionId': '2LWg7lQLnY41.maGB5Z6SWW.dcq0vx7b',
},
],
'ResponseMetadata': {
'...': '...',
},
}
The following example deletes objects from a bucket. The bucket is versioned, and the request does not specify the object version to delete. In this case, all versions remain in the bucket and S3 adds a delete marker.
response = client.delete_objects(
Bucket='examplebucket',
Delete={
'Objects': [
{
'Key': 'objectkey1',
},
{
'Key': 'objectkey2',
},
],
'Quiet': False,
},
)
print(response)
Expected Output:
{
'Deleted': [
{
'DeleteMarker': 'true',
'DeleteMarkerVersionId': 'A._w1z6EFiCF5uhtQMDal9JDkID9tQ7F',
'Key': 'objectkey1',
},
{
'DeleteMarker': 'true',
'DeleteMarkerVersionId': 'iOd_ORxhkKe_e8G8_oSGxt2PjsCZKlkt',
'Key': 'objectkey2',
},
],
'ResponseMetadata': {
'...': '...',
},
}