How to clear all messages in Discord channel using JDA bot

I’m working on a Discord bot with JDA and need to wipe out all messages in a specific channel. My current approach isn’t working properly and fails most of the time. Here’s what I have right now:

{
    List<Message> messageList;
    
    messageList = channel.getHistory().retrievePast(50).complete();
    channel.deleteMessages(messageList).queue();
}

The channel object is correct and the method gets called properly, but the deletion process is unreliable. Sometimes it works, sometimes it doesn’t do anything at all. What’s the right way to handle bulk message deletion in JDA?

yeah, the 14-day limit is def your problem. but also add some error handling with try-catch blocks. i had the same issue - turns out some messages cant be deleted (like system msgs) and it kills the whole batch. try looping through smaller chunks instead.

You’re probably not checking message ages before trying to bulk delete. Discord’s bulk delete API will reject the whole batch if any message is older than 14 days - that’s why you’re seeing inconsistent behavior. I hit this same issue building my first mod bot. Here’s what fixed it for me: grab messages in smaller batches and check timestamps first. Split them into recent vs old messages. Bulk delete anything under 14 days, then delete older messages individually. Also throw in some rate limit handling with CompletableFuture delays between batches - Discord doesn’t mess around with deletion spam. Your code structure looks good, just needs better message filtering and error handling for the age limits.

Your issue is probably Discord’s 14-day limit on bulk deletes. The API won’t bulk delete messages older than 14 days, and your code isn’t handling this properly. When it tries to delete old messages, it just fails silently or throws errors you’re not catching. Here’s what I’d do: filter messages by age first, bulk delete the new ones (under 14 days), then delete older messages one by one. You’ll also want to add proper error handling and throw in some delays between requests - you might be hitting rate limits, which would explain the inconsistent behavior.