# Nack All (/sdks/cpp/how-to/queues/nack-all)



## Overview [#overview]

**Bulk nack** rejects an entire polled batch of queue messages in a single call instead of settling each one individually. It's the operation you reach for when a failure affects the whole batch at once — a downstream dependency is down, a shared resource lock couldn't be acquired, or a transient error means none of the messages can be processed right now — and retrying them one-by-one would just be extra round-trips for the same outcome.

It works with manual-ack polling: `receiver->Poll` with `auto_ack = false` holds the returned messages without settling them, and `poll_result->NackAll()` sends one negative-acknowledge that settles every message in that result, returning them all to the queue for redelivery.

**Gotchas:** the receive count increments on every message in the batch, so an unbounded retry loop is one bad `NackAll()` away — pair it with `SetMaxReceiveCount` and a dead-letter policy. `NackAll()` is all-or-nothing: you can't use it to keep a few messages and reject the rest — that needs `AckAll()`/`ReQueueAll()` semantics or per-message settlement. And calling it on an empty result is a wasted round-trip, so guard on `!poll_result->messages().empty()` first.

## Prerequisites [#prerequisites]

* KubeMQ server running on `localhost:50000`
* C++ SDK installed (vcpkg or CMake FetchContent)
* C++17 compiler (GCC 9+, Clang 9+, MSVC 2019+)

## Code [#code]

```cpp title="main.cc"
// Example: queues_stream/nack_all
//
// Demonstrates rejecting all messages in a transaction using NackAll.
// Rejected messages are returned to the queue for reprocessing (up to
// the MaxReceiveCount limit, after which they are discarded).
//
// Channel: cpp-queues.nack-all
// Client ID: cpp-queues-nack-all-client
//
// Run with a KubeMQ server on localhost:50000
// (see https://docs.kubemq.io/deploy).

#include <kubemq/kubemq.h>

#include <iostream>

int main() {
    std::cout << "[1] Connecting to localhost:50000" << std::endl;

    kubemq::ClientOptions options;
    options.set_address("localhost", 50000);
    options.set_client_id("cpp-queues-nack-all-client");

    auto client_result = kubemq::Client::Create(options);
    if (!client_result.ok()) {
        std::cerr << "[ERROR] Failed to create client: " << client_result.status().message()
                  << std::endl;
        return 1;
    }
    auto& client = *client_result;

    std::string channel = "cpp-queues.nack-all";

    // Send a message with a receive policy so the server knows how to
    // handle it after rejection (re-deliver up to 3 times).
    std::cout << "[2] Sending message with max_receive_count=3" << std::endl;
    auto msg_result = kubemq::QueueMessage::Builder()
                          .SetChannel(channel)
                          .SetBody("will be nacked")
                          .SetMaxReceiveCount(3)
                          .Build();
    if (!msg_result.ok()) {
        std::cerr << "[ERROR] Build message: " << msg_result.status().message() << std::endl;
        return 1;
    }
    auto send_result = client->SendQueueMessage(*msg_result);
    if (!send_result.ok()) {
        std::cerr << "[ERROR] SendQueueMessage: " << send_result.status().message() << std::endl;
        return 1;
    }
    std::cout << "[3] Message sent" << std::endl;

    // Receive the message via downstream receiver.
    std::cout << "[4] Opening downstream receiver" << std::endl;
    auto receiver_result = client->NewQueueDownstreamReceiver();
    if (!receiver_result.ok()) {
        std::cerr << "[ERROR] NewQueueDownstreamReceiver: " << receiver_result.status().message()
                  << std::endl;
        return 1;
    }
    auto& receiver = *receiver_result;

    kubemq::PollRequest poll_req;
    poll_req.channel = channel;
    poll_req.max_items = 10;
    poll_req.wait_timeout_seconds = 5;
    poll_req.auto_ack = false;

    auto poll_result = receiver->Poll(poll_req);
    if (!poll_result.ok()) {
        std::cerr << "[ERROR] Poll: " << poll_result.status().message() << std::endl;
        return 1;
    }
    if (poll_result->is_error()) {
        std::cerr << "[ERROR] Poll response: " << poll_result->error() << std::endl;
        return 1;
    }

    std::cout << "[5] Received " << poll_result->messages().size() << " messages" << std::endl;
    for (const auto& dm : poll_result->messages()) {
        std::cout << "  body=" << dm.message().body() << " tx=" << dm.transaction_id() << std::endl;
    }

    // NackAll: reject all messages in the transaction (return to queue).
    if (!poll_result->messages().empty()) {
        std::cout << "[6] NackAll: rejecting all messages" << std::endl;
        auto nack_status = poll_result->NackAll();
        if (!nack_status.ok()) {
            std::cerr << "[ERROR] NackAll: " << nack_status.message() << std::endl;
            return 1;
        }
        std::cout << "[7] All messages rejected (returned to queue)" << std::endl;
    }

    // Close resources explicitly.
    // Note: Destructors also handle cleanup, but explicit calls are shown
    // for clarity and to match Go's defer pattern.
    receiver->Close();

    auto close_status = client->Close();
    if (!close_status.ok()) {
        std::cerr << "[ERROR] Close failed: " << close_status.message() << std::endl;
        return 1;
    }
    std::cout << "[8] Client closed" << std::endl;

    return 0;
}
```

## How It Works [#how-it-works]

* Receives messages and negative-acknowledges all with `poll_result->NackAll()`.
* Nacked messages are returned to the queue for redelivery.
* Useful when a batch of messages cannot be processed (e.g., downstream unavailable).
* Compare with `AckAll()` and `ReQueueAll()` for other settlement patterns.

## Related [#related]

* [Pattern overview](/learn/queues/getting-started)
* [C++ SDK Reference](/sdks/cpp/reference/queues)
* [Ack Range](/sdks/cpp/how-to/queues/ack-range)
* [Requeue All](/sdks/cpp/how-to/queues/requeue-all)
