JDA Discord bot to delete all messages from a text channel

Viewed 878

I am trying for my JDA discord bot to delete all the messages from a text channel and I am using a highly inefficient code as of right now which occasionally works and occassionally doesn't. The code:

    {
        List<Message> msgs;

        msgs = textChannel.getHistory().retrievePast(50).complete();
        textChannel.deleteMessages(msgs).queue();
    }

I know for a fact that I am calling the function and I know for a fact that the textChannel being passed is the correct one. Please do help me with the same.

2 Answers

The fastest and most efficient way I could think of, would be to clone the Channel and delete the old one.

Try something like this:

textChannel.createCopy().queue();
textChannel.delete().queue();

Here is a solution to your problem:

MessageHistory history = MessageHistory.getHistoryFromBeginning(channel).complete();
List<Message> mess = history.getRetrievedHistory();
for(Message m: mess){
   m.delete().queue();
}
Related