Search code examples
amazon-web-servicesamazon-sqsaws-php-sdk

AWS SQS Delete Batch Message, Error: TooManyEntriesInBatchRequest


I am trying to delete AWS SQS Batch messages at a time, it deletes first 10 messages of array chunk but not deleting 2nd chunk, throw error

AWS.SimpleQueueService.TooManyEntriesInBatchRequest (client): Maximum number of entries per request are 10. You have sent 12.

I am following these steps.

I have array of 12 messages, and converted them into chunks like this

$chunks = array_chunk($entries, 10, true); // make chunks of 10 items per array

Chunk 1


Array
(
    [0] => Array
        (
            [Id] => 85d6aea0-7903-41d4-8f5a-efba992e8868
            [ReceiptHandle] => AQEBRW1MH1XnKD0WMLl1xtJqLpJSeEpgephGcaRTqzz+FdOAXOnYMTcG46+SpNp79cDoH1vnxPswcpuk5lK9HpHaBe8jMx3ILliCPnDieSamD84H0gz+hMgqRauqjw6jTZ/r+vkF+1AzIKo7JcxlG4IqIFXkoQCQlAsdBqALlQbb3y9aSwv9ZrBCc74x+4b7r8TvagFEbN4O984N7/wRh9AvMV/n4MrESePKjOtks53ACBPTf6c4AuBNrnbOvLCABV10KoDRpHhSC9bxQPbR/gM814+utyvRKEjk5kS0sAtUzcmgVkms/iS5vF70ImiVlWnaA2TO82G34zA9g52rRRxeSdpp+LTwMMNPExCxb1TLgyVGp1ROPfwzK7woXMDvmXaHVfVTbp/hLVCLA3O6x2U7aw==
        )

    [1] => Array
        (
            [Id] => 1f2f5165-56a1-4df4-a70a-d22869b408ec
            [ReceiptHandle] => AQEBJlSHjWdNtHzY3WtGpJW+0IQkoF51Pq+hookKBbc9oGTPsm1h9Nm+Y6BphhgbXzAHDpRoOPc7OSQEBOEYC4l4vjgXmOHHVve6lfQqPcZd3zhdFJzy5ZWgP00WIzurOi55j+EHaFRBS1SEx6Tj6rx4AJ/Nuvri8hmBYctbyQzhZKgIVYdviTXhkxzdWHsZ2zduWHU5AWex3Z+xgH3xTrRPUoO7Bsqun+QHS0lqS93D6TejHnhKGJO3Q5cUJA0Lo0iEF9rND4ha0Ar9hyAGseGwqHjQYr0279x1e5KuVnRip6pj3WBiLW7CjW3+XqRLRFSaquIHMqUwp4HcamTjUNSzMFhtHRPRi1w28+tPQ3bswV6b3YMXFc40NQzHzQVn5y/839+xk00UlIKe9SIh9qYecQ==
        )

    [2] => Array
        (
            [Id] => 726b8b47-be1f-4aea-aa30-f24d08361d52
            [ReceiptHandle] => AQEBHYsYOYJZJV4dMvzprs2v/r3+SGQoxaEYvIfHpwvHkHpPZ7hIgGfhEpZ8mfSJs7iAKETt5KEuyWTbEvIhijX2SjPOWOXZgkvkZ0yAR/sTnLNng3ZwvP7qsLL50FjBBDgAyRCeN0LMdZJ4LApTdCAaeU+5XOnEgpImBeclp7rzlfONofsyyLl7ifuozfzdWz0PhAsIUqLaQzxzvKbOBBZt6lgVgGGDucuduMpcxugarr71N/IGHt00orJMFRhkorvIoNxp1FFbUzFM0izX8Z5/lghFywbXBafoc8TMGp4L+K7ZPRB/T7NNGUKEtTBydbOk2KoQXfFoshV15LE5zT/NLt9UVHbZsjl1yUofxolqEnH0xpi2KCbJXttImaTK7TXI2CzG1ZK0KdBLCGnaCKW7ZA==
        )

    [3] => Array
        (
            [Id] => ed365b40-d6c7-4e8a-afd1-9489a8e4706a
            [ReceiptHandle] => AQEBUW6iS/PXNzxciJL2rikTumcgmbVM+aILQQQ99/XxBgOtV+LY6y1qCcbH1TRqk8hv0wzihN/JUnXxik40eF0xHtwMjiay7uFsuAAhorGIh7376IgqK+pPNpHtTjcABCIalt1C2MiugSgy0D3sxyis0pDz7GuN7oVxfvppVPVpnS/3i59WszsiHhNoNEtW3xAZ9EOY3qIlIEG0caPCKPfEAJf9kAUfTPSB45lUve8sjcDk2EGRp0BiwnJ4SYceQLU4eVc2VRky9znK7iDIbCyzWfopnMrjk5zONTcXIa0KixJXlsOzUUdPETMt5WafXUHc3vrvLb7g3zWEo9+W+NeJ4ODwGKrWlBh75ckdIb7ZuADtstINkgfBwTNMOgox+Z9Qi45iJNb3TvHFEFSZ6BUuuw==
        )

    [4] => Array
        (
            [Id] => 48c4f212-1375-40f0-abcc-1247bef9b89d
            [ReceiptHandle] => AQEB/xROTQvGFdWzP1tUAt75os/f29QzJ1OdXJzIV/24/MmE0HXJlZwAg4EwbO+OQ8EATvrSxJYeO5Y11WToOcTM194f5AnJ1mL2Ul3Zom3OmeiDEqptUz5NsM9jOPQaf/gaHr0V7++8gCosn2UO5+21DdYy0WfAEL3U1gAgB2z5IN/HPQnXg7h5JyxRE/o7BMWs+doAdk/sl5Y82qp1MRVAS8/t6uVF/JTlaPmaeMfeysMKmOEe+OnSibYX0P+KzOzwfC4GmNDh6wepmsZ1PG9IMoKik0bYa0FxFJfPYO81wEtihEqOVUFj8vv+msSRZ+J+uFNCS7ikdAJMWnL0w7AJfj4jWgK5TcThUmjWa9XY40kcNCv0jEiKQwh9S84koPucwbn9ZqRUvtcOD+xXJJiEuQ==
        )

    [5] => Array
        (
            [Id] => 604db975-48da-48ab-bd43-334c67cd8450
            [ReceiptHandle] => AQEBdoaZFWmTiFmbG7HrX6RoOLTDb45Fsyo6VEgDZb+x01glh4ewQ12emwzSAXsoodM5jP5NOGsqACFxY1rgUFsKP4S2Qi2pEw80X1BwXfrETTp2axwLlrws9I5YoHXqm+D/tJRlSc6whQqS6S1hRnWYAVaTllDDi+zDvi+3dBnStmgRmF3V3FTc8VJZ6PGdrd4UF3UpMzUylQhQndf/vxnnUI6qXd7IjOSZLwoohr0jYRLUSc4QZrp4T4itFgYF4kVSLtB7HCvUhdWt90zR8n4QW7WbTHfhOxRD9gIelFmzl/ssjS85eZ+ZnIxSZiJnJ13DGGNBo5POOHXqk0MOqC7dYzyaW+NOC/C2pBD2XkjG0fDStftb5KuAo+tjtMfCvJAEOtqAQsRXVb9/MN/zhAEPSA==
        )

    [6] => Array
        (
            [Id] => e8525043-7011-4d2b-965e-a844f21ade81
            [ReceiptHandle] => AQEBzJZiFcePez9hVhoUmY4ccH758aIfxwM0QkUmIoPlrvyPJGOsQT9GeSPVCjYhCBVlMikj/VohnfANAS0puVsAxwR8sMxhZhrALg2D2nnY2u40d3ktM7YKLhut/FQLxSw7OEqo3NoW3akQdusuWSYKRkb6aLICYhLfJch2x7BPaUFxB6gM4PkPZj9yeocX8KP2+TMPPmT6lESGmq1TjDIXmw0A+eFIWEOr/5k66wbGhDUArrglqKlhJgI15k8k4f5W11LGEJUPVWazzUoCHhzmXiEB3hvfzlzKRQwhdZqM4YHAVZsqrPfcQth3dpdGW6p8yEaco+yBJ13rNZD7EYGARpzMSK3WEU18zbc33SphPHVPewYgtrqNCFev2L/Wfe3mpEN13FLNbUrU1cFRrd7xSw==
        )

    [7] => Array
        (
            [Id] => 30ec6108-e3c3-4348-b449-00775f18e579
            [ReceiptHandle] => AQEBOGx1ntCVZUGvNvqmk9IQZTUOXa5XTYYlMQatmWEOX0GKfNSq4P1hOcFWgEAhp3C7hFyaTrV+8w5+r+evw6+A80JV1ZKwlSTCti7riYXz7cJDsU6HXvIwrrzZmCBLc1WH6kR983Hi/H9pHqeaB1nOlRLG6jUiQKlLdAVQHVGIDXqmuALGLMTIOiOIzZmcnqhbKiHVJ53lggi/6CCSLe75ofSrFf/dAZCABi8pkuFceOJdcQ8M3qUuwehR879784cnLDVADq6QeEqB9acxINra/MKOo+YyUP0My+49yRJJhjb1vORy3HV56ZsZGNf8tWHuqBSI1aTpPgCUKVGLlId61LHjXMIYlv6e1myY5imt5RLOLleLTq0vQxGtU1bjL+ixOdzLqC571uOIgyEvLkz47Q==
        )

    [8] => Array
        (
            [Id] => 06d0c6a5-d266-4f64-8c1a-fcb12e94d1aa
            [ReceiptHandle] => AQEB8d+rJyCEGFfw9zNh4lCS1MNxDC9Zdk6ARJjH7ff9gnZzcwWFvuq5JwsWaItY11dDFWowbzY6Xk1Jg4ii2RvViAqEg86dSfK9N0WstWe4yRYv0thVqSv1BuESymoSnaljcmamh6HfxuFHwkiyYFTtc2oJ3YRrWMlAYaSj8cviZPTLb7Q1zc47UlTfEGdpC4KSQ65mPg4ZutlbZQtEzXRZtwMbNm4tFciKBVpK+5N1NcNTi5S+6Nd4stS14q/a928urssO/Gewh7tDXXndZ3PtCEqGKC18xHHMenY5g2xhZT6NsZxu8+lPX8WMS9HB+lwbZeYBWNu/zqSPop/UXJrXSzHIda3beYl1N11vDAXWnfDS1vYOev4NQkeuOVJd6XJso6hGDo7cb/CEtwzwynw19Q==
        )

    [9] => Array
        (
            [Id] => 36563e1b-a4a7-4d65-ad89-d5357d1c06a6
            [ReceiptHandle] => AQEB0d//nx/AQGOrERVynoRxtbwaerPNHy6vurGAzJ+S7FMf0X5HS3BGNVkUk57GhssgcAiQ1XJ/8iqaZ3NVlER3OXttgjME2bYz+ji/ermvAZCazmP6DsQSqh7caGxIR1HcsAsXksp3nRefEbqoziBPH15xXyhP45SiE6xLBsiIbvP10JMPmDXbhVRBugMf3lYNrXP55ZKf8O0Nm+DrSmk2mckevwugY0SmiGIjwV5+uOIIoKsen37WU0mnDX883jji4GH+TWyE95wHlzRvSa2OZdMWt4s66tyTyVo3U5Ji/9RyUhceNQJZxyKUf68WBeOiymd1TVwzkiJhdmXb5wSteOcZZX2oH+feyOeOBxPwpKbK05ttBNVdf3IayJQvbtUu4q58dkLDdOnJH+m/MC+vyA==
        )

)

Chunk 2

Array
(
    [10] => Array
        (
            [Id] => 1f2f5165-56a1-4df4-a70a-d22869b408ec
            [ReceiptHandle] => AQEByG1UiIz/+Uqlb3R5gkfVqOzNrWPYq8YEc3XluoqYJpVQD13TcpeFAPNU4C0NybzsKtRLLQfsM58dUehrlGP5JmU9H+ROgAGDF5R6qVNyot+dJtRKmUJdmt2A2r6NkiYMg7hgrU4+upRMhTlOAY/Ni5HGOvJbhvR/upHKSVe5gWoabQTG6Puvvg8L/pamET+PcImDxQPgSNzLc9L/8SwhTwYyqdYG4CwhymelqiAtI7siHpyBBmC24JyX6IWL/3pJenI0yBZhREdNsd2hHLcZAWoCrhq4EFsneAgcnm9UWOCuorceCOQHU94trl38z9tbXXbpEjI5ZVWlrhc6Abx6vknegjIdAoR2gLwkvUdSTHkV4QxoFZOKjLoYJBeV2ugaHgJi/52/DdL9HNQoQziwZg==
        )

    [11] => Array
        (
            [Id] => 726b8b47-be1f-4aea-aa30-f24d08361d52
            [ReceiptHandle] => AQEBcWCMm29Cic5pJH+1I0ppsiQxjQw9Q0znj26PZ2P1YQqu5tMvTsCX6t63JgoYvxzH34Ss6oXxzuXe8vXiIVoj7kSFiZdBnPVq+tcEoEFnCfBOtg2WQVONWlN5JYulAIewxXbQS2JZJjIu4HWHHzhS0oRw62OMz325/pbcuaW2a1mfU+Zs7bF9eK6AOCl4RJ0VBACSodpNJtlyqOxnS7C6zkuuVERGuQIxhFFTx44pAHzvQ3+WklGkY05QNNdxUaGzvQGhqzlwFiPwhBi6Cg22PgrSwnSt8Aef1azykibngUP035LOi1ig6Tc5jrWFX39MN+UYt6AVBTELxv4uTIi1VFJue8f8mRxJSL4H+YQH0BBy/zxPAJZANK4E066jbxtgffk8ktJcZOGBCUuFeFSrVA==
        )

)

Then Loop through the Entries and making delete action

//loop through and delete one by one
foreach($chunks as $k=>$entry_chunk){

    $result = $this->Sqs->deleteMessageBatch([
        'Entries' => $entry_chunk,
        'QueueUrl' => $queueUrl
    ]);
}

This deletes the 1st Chunks (of 10 items) while 2nd comes into, it give me error

AWS.SimpleQueueService.TooManyEntriesInBatchRequest (client): Maximum number of entries per request are 10. You have sent 12.

How I can delete multiple messages at a time?


Solution

  • $chunks = array_chunk($entries, 10, true);
    

    That third argument seems wrong, because of this, in the second chunk:

     [10] => Array
    

    The 0th element of the second chunk shouldn't have index 10, should it? Should be 0. That seems to imply there are 10 undefined elements preceding it, so you're still apparently trying to delete 12 messages in one batch... 10 invalid, undefined ones and the 2 remaining ones. Because of this error, SQS may not be getting around to telling you that the first 10 messages don't even make sense, because you've exceeded the maximum messages per request -- which is 10 -- and the request is therefore already invalid.

    The third argument $preserve_keys is set to true, but should be false.

    preserve_keys

    When set to TRUE keys will be preserved. Default is FALSE which will reindex the chunk numerically

    http://php.net/manual/en/function.array-chunk.php