Sample code for 30+ languages & platforms
Zig

SQS Delete Message

See more Amazon SQS Examples

Deletes a message from an SQS queue. You specify the message by using the message's receipt handle and not the message ID you received when you sent the message. This example will first receive a message, and then use the receipt handle to delete it.

See SQS DeleteMessage or detailed information.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect to the Amazon AWS REST server.
    // such as https://sqs.us-west-2.amazonaws.com/
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    try rest.connect("sqs.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect);

    // Provide AWS credentials for the REST call.
    const auth_aws = try chilkat.AuthAws.init();
    defer auth_aws.deinit();
    auth_aws.setAccessKey("AWS_ACCESS_KEY");
    auth_aws.setSecretKey("AWS_SECRET_KEY");
    // the region should match our URL above..
    auth_aws.setRegion("us-west-2");
    auth_aws.setServiceName("sqs");

    rest.setAuthAws(auth_aws) catch {};

    rest.addQueryParam("Action", "ReceiveMessage") catch {};
    rest.addQueryParam("MaxNumberOfMessages", "5") catch {};
    rest.addQueryParam("VisibilityTimeout", "15") catch {};
    rest.addQueryParam("AttributeName", "All") catch {};
    rest.addQueryParam("MessageAttributeName", "All") catch {};

    // Use the actual path part of your SQS queue URL here:
    var response_xml: [:0]const u8 = rest.fullRequestNoBody(alloc, "GET", "/123456789123/chilkatTest") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // A successful response will have a status code equal to 200.
    if (rest.getResponseStatusCode() != 200) {
        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response body: {s}\n", .{response_xml});
        return;
    }

    // Examine the successful XML response.
    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.loadXml(response_xml) catch {};

    // A successful response looks like this:
    // <?xml version="1.0" encoding="utf-8" ?>
    // <ReceiveMessageResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/">
    //     <ReceiveMessageResult>
    //         <Message>
    //             <Body>this is a test</Body>
    //             <MD5OfBody>54b0c58c7ce9f2a8b551351102ee0938</MD5OfBody>
    //             <ReceiptHandle>AQEBMyLof...UbKBTvHtEg==</ReceiptHandle>
    //             <Attribute>
    //                 <Name>SenderId</Name>
    //                 <Value>957491831129</Value>
    //             </Attribute>
    // ...
    // ...
    //             <MessageId>52882c65-5e21-4450-9024-93a3b01e61d3</MessageId>
    //             <MD5OfMessageAttributes>2ff68603f4239272bd03f543254ed040</MD5OfMessageAttributes>
    //         </Message>
    //     </ReceiveMessageResult>
    //     <ResponseMetadata>
    //         <RequestId>05ba5c92-80ee-5f87-be76-0cf67b7475d6</RequestId>
    //     </ResponseMetadata>
    // </ReceiveMessageResponse>

    // Get some values from the XML:
    const receipt_handle = try xml.chilkatPath(alloc, "ReceiveMessageResult|Message|ReceiptHandle|*");
    std.debug.print("ReceiptHandle = {s}\n", .{receipt_handle});

    // Now use the receipt handle to delete the message.
    // Use the same connection (or automatically reconnect if needed)
    rest.clearAllQueryParams() catch {};

    rest.addQueryParam("Action", "DeleteMessage") catch {};
    rest.addQueryParam("ReceiptHandle", receipt_handle) catch {};

    // Use the actual path part of your SQS queue URL here:
    response_xml = rest.fullRequestNoBody(alloc, "GET", "/123456789123/chilkatTest") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // A successful response will have a status code equal to 200.
    if (rest.getResponseStatusCode() != 200) {
        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response body: {s}\n", .{response_xml});
        return;
    }

    // Examine the successful XML response.
    xml.loadXml(response_xml) catch {};
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // A successful DeleteMessage response looks like this:
    // <DeleteMessageResponse>
    //     <ResponseMetadata>
    //         <RequestId>
    //             b5293cb5-d306-4a17-9048-b263635abe42
    //         </RequestId>
    //     </ResponseMetadata>
    // </DeleteMessageResponse>
}