deleteObjects method

Future<DeleteObjectsOutput> deleteObjects({
  1. required String bucket,
  2. required Delete delete,
  3. bool? bypassGovernanceRetention,
  4. String? expectedBucketOwner,
  5. String? mfa,
  6. RequestPayer? requestPayer,
})

This operation 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 operation 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 operation 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 operation supports two modes for the response: verbose and quiet. By default, the operation 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 operation encountered an error. For a successful deletion, the operation does not return any information about the delete in the response body.

When performing this operation 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:

Parameter bucket : The bucket name containing the objects to delete.

When using this API 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 operation with an access point through the AWS 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 Simple Storage Service Developer Guide.

When using this API 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 operation using S3 on Outposts through the AWS SDKs, you provide the Outposts bucket ARN in place of the bucket name. For more information about S3 on Outposts ARNs, see Using S3 on Outposts in the Amazon Simple Storage Service Developer Guide.

Parameter delete : Container for the request.

Parameter bypassGovernanceRetention : Specifies whether you want to delete this object even if it has a Governance-type Object Lock in place. You must have sufficient permissions to perform this operation.

Parameter expectedBucketOwner : The account id of the expected bucket owner. If the bucket is owned by a different account, the request will fail with an HTTP 403 (Access Denied) error.

Parameter mfa : The concatenation of the authentication device's serial number, a space, and the value that is displayed on your authentication device. Required to permanently delete a versioned object if versioning is configured with MFA delete enabled.

Implementation

Future<DeleteObjectsOutput> deleteObjects({
  required String bucket,
  required Delete delete,
  bool? bypassGovernanceRetention,
  String? expectedBucketOwner,
  String? mfa,
  RequestPayer? requestPayer,
}) async {
  ArgumentError.checkNotNull(bucket, 'bucket');
  ArgumentError.checkNotNull(delete, 'delete');
  final headers = <String, String>{
    if (bypassGovernanceRetention != null)
      'x-amz-bypass-governance-retention':
          bypassGovernanceRetention.toString(),
    if (expectedBucketOwner != null)
      'x-amz-expected-bucket-owner': expectedBucketOwner.toString(),
    if (mfa != null) 'x-amz-mfa': mfa.toString(),
    if (requestPayer != null) 'x-amz-request-payer': requestPayer.toValue(),
  };
  final $result = await _protocol.sendRaw(
    method: 'POST',
    requestUri: '/${Uri.encodeComponent(bucket)}?delete',
    headers: headers,
    payload: delete.toXml('Delete'),
    exceptionFnMap: _exceptionFns,
  );
  final $elem = await _s.xmlFromResponse($result);
  return DeleteObjectsOutput(
    deleted: $elem
        .findElements('Deleted')
        .map((c) => DeletedObject.fromXml(c))
        .toList(),
    errors: $elem.findElements('Error').map((c) => Error.fromXml(c)).toList(),
    requestCharged: _s
        .extractHeaderStringValue($result.headers, 'x-amz-request-charged')
        ?.toRequestCharged(),
  );
}