How to create a channel clearing command for Discord bot in Python

I’m trying to build a command that will delete all messages from a Discord channel when triggered. My current attempt doesn’t seem to work at all and the bot just ignores the command completely.

@bot.command()
async def clear_all(ctx):
    await ctx.channel.purge()
    await ctx.send("Channel has been cleaned successfully!")

What I want is for the bot to wipe out every message in the current channel when someone types the command. Right now nothing happens when I try to use it. Am I missing something obvious here?

Your code’s almost right, but it’s probably a permissions thing. The bot needs ‘Manage Messages’ permission to purge messages. Also, you should add a limit parameter; using purge() without any arguments can be unreliable.

Try this code instead:

@bot.command()
async def clear_all(ctx):
    await ctx.channel.purge(limit=None)
    await ctx.send("Channel has been cleaned successfully!")

Make sure your bot has the correct permissions set in the server. If it lacks ‘Manage Messages’ permission, the purge command will fail silently, which is likely the issue you’re encountering.

first, check if ur bot’s online and responding to other commands. sometimes it’s not a permissions issue - could be the wrong command prefix or the bot’s just offline. also, add @commands.has_permissions(manage_messages=True) above ur command so only users with the right permissions can use it.

This is definitely a bot permissions issue. Had the same problem with my moderation bot last year. Discord needs specific permissions for bulk deletion, plus there’s a 14-day limit - you can’t purge messages older than that with the standard method. Your bot needs both ‘Manage Messages’ AND ‘Read Message History’ permissions. Without the second one, purge operations just fail silently which is super annoying. Also throw in some error handling so users actually know when it fails. The purge method automatically skips messages over two weeks old, so if your channel has ancient messages, they’ll stay put and make it look like the command’s broken.