Delete a batch of messages from a queue in a single request. :param queue: The queue from which to delete the messages. :param messages: The list of messages to delete. :return: The response from SQS that contains the list of successful and failed message deletions.
(queue, messages)
| 152 | |
| 153 | # snippet-start:[python.example_code.sqs.DeleteMessageBatch] |
| 154 | def delete_messages(queue, messages): |
| 155 | """ |
| 156 | Delete a batch of messages from a queue in a single request. |
| 157 | |
| 158 | :param queue: The queue from which to delete the messages. |
| 159 | :param messages: The list of messages to delete. |
| 160 | :return: The response from SQS that contains the list of successful and failed |
| 161 | message deletions. |
| 162 | """ |
| 163 | try: |
| 164 | entries = [ |
| 165 | {"Id": str(ind), "ReceiptHandle": msg.receipt_handle} |
| 166 | for ind, msg in enumerate(messages) |
| 167 | ] |
| 168 | response = queue.delete_messages(Entries=entries) |
| 169 | if "Successful" in response: |
| 170 | for msg_meta in response["Successful"]: |
| 171 | logger.info("Deleted %s", messages[int(msg_meta["Id"])].receipt_handle) |
| 172 | if "Failed" in response: |
| 173 | for msg_meta in response["Failed"]: |
| 174 | logger.warning( |
| 175 | "Could not delete %s", messages[int(msg_meta["Id"])].receipt_handle |
| 176 | ) |
| 177 | except ClientError: |
| 178 | logger.exception("Couldn't delete messages from queue %s", queue) |
| 179 | else: |
| 180 | return response |
| 181 | |
| 182 | |
| 183 | # snippet-end:[python.example_code.sqs.DeleteMessageBatch] |
no test coverage detected