如何使用discord.js删除文本频道中的所有消息?
Hey, I totally get the frustration of trying to wipe a Discord channel clean and not finding clear steps. Let’s walk through all the viable ways to delete every single message in a channel, based on what you can access and your needs:
If you’re an admin or have the Manage Messages permission, Discord has a native tool—but it has a hard limit: it can only delete messages sent within the last 14 days. Here’s how to use it:
- Right-click the channel you want to clear
- Select Bulk Delete Messages
- Choose to delete either your own messages or all messages (the latter requires elevated permissions)
- Confirm the action, and Discord will wipe all eligible messages.
This is great for recent clutter, but useless for older archives.
To delete every single message—no matter how old—your best bet is a custom, self-hosted Discord bot. This gives you full control and avoids relying on risky third-party tools. Here’s a step-by-step breakdown:
Step 1: Set Up a Bot
- Head to the Discord Developer Portal, create a new application, and add a bot user to it.
- Grant the bot the Manage Messages and Read Message History permissions, then invite it to your server using the generated invite link.
Step 2: Write a Simple Purge Script
Below’s a quick example using Discord.js (Node.js):
First, install the library via npm:
npm install discord.js
Then create a purge.js file with this code:
const { Client, GatewayIntentBits } = require('discord.js'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent ] }); client.on('ready', () => { console.log(`Logged in as ${client.user.tag}! Ready to purge channels.`); }); client.on('messageCreate', async (message) => { // Only let admins run this command if (message.content === '!clearall' && message.member.permissions.has('Administrator')) { const channel = message.channel; let deletedMessages; // Bulk delete in batches of 100 (Discord's max per request) do { deletedMessages = await channel.bulkDelete(100); } while (deletedMessages.size > 0); // Send a confirmation that auto-deletes after 3 seconds const confirmation = await channel.send('✅ All messages in this channel have been deleted!'); setTimeout(() => confirmation.delete(), 3000); } }); // Replace with your bot's token from the Developer Portal client.login('YOUR_BOT_TOKEN_HERE');
Run the script with node purge.js, then type !clearall in the target channel (as an admin) to trigger the purge.
Note:
This script will keep deleting batches of 100 messages until the channel is completely empty—works for messages of any age.
There are some third-party tools that claim to clear entire channels, but I’d strongly advise against most of them:
- Many require you to share your bot token or account credentials, which is a huge security risk (your account/server could get hacked).
- A lot of these tools violate Discord’s Terms of Service, which could result in your account or server being banned.
If you absolutely must use one, do your research thoroughly and only use tools from trusted sources—but self-hosting a bot is far safer.
Critical Pre-Purge Reminder
Before wiping a channel, export and save any important message history! You can do this via Server Settings > Privacy & Safety > Request Data Export to download a copy of all channel messages.
内容的提问来源于stack exchange,提问作者Johannes




