Last message will not get deleted

async function fetchAllMessages(chan) {
    const channel = client.channels.cache.get(chan);
    let messages = [];
  
    // Create message pointer
    let message = await channel.messages
      .fetch({ limit: 1 })
      .then(messagePage => (messagePage.size === 1 ? messagePage.at(0) : null));
  
    while (message) {
      await channel.messages
        .fetch({ limit: 100, before: message.id })
        .then(messagePage => {
          messagePage.forEach(msg => {
            messages.push(msg)
            msg.delete()
          });
  
          // Update our message pointer to be last message in page of messages
          message = 0 < messagePage.size ? messagePage.at(messagePage.size - 1) : null;
        })
    }
}

Hello! I use this function to clear a channel but he always not delete the last message that was posted in the channel why?
Was this page helpful?