Skip to content

Instantly share code, notes, and snippets.

@IMcPwn
Forked from niahoo/delete-all-messages.js
Last active July 12, 2023 19:41
Show Gist options
  • Star 93 You must be signed in to star a gist
  • Fork 41 You must be signed in to fork a gist
  • Save IMcPwn/0c838a6248772c6fea1339ddad503cce to your computer and use it in GitHub Desktop.
Save IMcPwn/0c838a6248772c6fea1339ddad503cce to your computer and use it in GitHub Desktop.
Delete all messages in a Discord channel
// Turn on Developer Mode under User Settings > Appearance > Developer Mode (at the bottom)
// Then open the channel you wish to delete all of the messages (could be a DM) and click the three dots on the far right.
// Click "Copy ID" and paste that instead of LAST_MESSAGE_ID.
// Copy / paste the below script into the JavaScript console.
// If you're in a DM you will receive a 403 error for every message the other user sent (you don't have permission to delete their messages).
var before = 'LAST_MESSAGE_ID';
clearMessages = function(){
const authToken = document.body.appendChild(document.createElement`iframe`).contentWindow.localStorage.token.replace(/"/g, "");
const channel = window.location.href.split('/').pop();
const baseURL = `https://discordapp.com/api/channels/${channel}/messages`;
const headers = {"Authorization": authToken };
let clock = 0;
let interval = 500;
function delay(duration) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), duration);
});
}
fetch(baseURL + '?before=' + before + '&limit=100', {headers})
.then(resp => resp.json())
.then(messages => {
return Promise.all(messages.map((message) => {
before = message.id;
return delay(clock += interval).then(() => fetch(`${baseURL}/${message.id}`, {headers, method: 'DELETE'}));
}));
}).then(() => clearMessages());
}
clearMessages();
Copy link

ghost commented Mar 8, 2019

Doesn;t work. I still see my messages. even after reloading or logging in again

Tested mine? Worked still weeks ago, since there I do it with the autohotkey program.

@cedws
Copy link

cedws commented Mar 8, 2019

I've been working on another solution for Discord message deletion.
https://github.com/c-edw/discord-delete

Copy link

ghost commented Mar 10, 2019

https://github.com/c-edw/discord-delete

So this thing delete EVERYTHING in discord?
Would be a good solution when I creating new Account.

@JoannaMakary
Copy link

This script used to work for me until a few updates ago.

Uncaught (in promise) TypeError: messages.map is not a function
at fetch.then.then.messages (:20:31)

is the error I started to get recently.

@simplycontent
Copy link

I've been working on another solution for Discord message deletion.
https://github.com/c-edw/discord-delete

Is this legit? I like the idea of it!

Copy link

ghost commented Mar 21, 2019

https://gist.github.com/z861gz6wb2/fd89c6cf9db9662c68698736d937f0d2
Hey guys, I updated the Discord "mass message deletion browser script" with LOTS of improvements and bug fixes. I rewrote most parts of the script. Now you don't even need to fill in your auth token, user id, channel id. The message id part is optional. My script fetches all the needed information for you so all you gotta do is copy and paste. It works anywhere, in channels and DMs. It even lets you know when it's finished deleting all the messages, so just chill until you get the alert. I made the alert tell you how many messages it deleted too so lots of good stuff. Feel free to look at the code and tweak it anyway you like. You can even turn it into a bookmark for easy launching, just use the "javascript:" prefix followed by the code (run my script through a JS minifier first) then put it in the URL box when adding it as a bookmark.

EDIT: People are reporting that this script fails to grab the auth token in chromium browsers like Opera and Brave. However I tested it in Chrome Canary and it worked. So if you don't want to use Chrome, you can fill in your token manually.

@testing123the
Copy link

testing123the commented Mar 22, 2019

https://gist.github.com/z861gz6wb2/fd89c6cf9db9662c68698736d937f0d2
Hey guys, I updated the Discord "mass message deletion browser script" with LOTS of improvements and bug fixes. I rewrote most parts of the script. Now you don't even need to fill in your auth token, user id, channel id. The message id part is optional. My script fetches all the needed information for you so all you gotta do is copy and paste. It works anywhere, in channels and DMs. It even lets you know when it's finished deleting all the messages, so just chill until you get the alert. I made the alert tell you how many messages it deleted too so lots of good stuff. Feel free to look at the code and tweak it anyway you like. You can even turn it into a bookmark for easy launching, just use the "javascript:" prefix followed by the code (run my script through a JS minifier first) then put it in the URL box when adding it as a bookmark.

I replied to your code that I'm getting this error that it cannot find the IDs:
"VM369:20 Uncaught TypeError: Cannot read property 'replace' of undefined
at :20:62
at :84:3"

EDIT:
I can confirm it works if you do it manually, as the Authtoken just appears for a brief 1 sec when you load the page and the script cannot find it afterwards when it runs.
Manually:

  1. Simply use the original method to catch it (I used screenshot and typed it in manually, cause I'm too slow to copy+paste or it doesn't work?!).
  2. Paste/type it here <<const authToken = frame.contentWindow.localStorage.token.replace(/"/g, "");>>, meaning you replace <<frame.contentWindow.localStorage.token.replace(/"/g, "")>> with <<"YOUR_AUTHTOKEN">>
    (I used <<>> to quote, so remove those and don't forget to add the "")
  3. Thank the guys that helped with the code! <3

@victornpb
Copy link

victornpb commented Apr 8, 2019

UPDATE: 2019-APRIL

I wrote a mass delete script properly, it logs the messages being deleted along with a progress on a popup.
it also gives you an estimated time!

https://gist.github.com/victornpb/135f5b346dea4decfc8f63ad7d9cc182

@thegohst
Copy link

thegohst commented Jun 3, 2019

There another solution , using android mobile application to delete the messages , it work , i tested it

https://play.google.com/store/apps/details?id=com.mlika.discorddelete.free

@TheOutride
Copy link

Please be EXTREMELY careful of downloading applications. Make sure you trust them with the permissions you give them.

Code currently working found here: https://github.com/TheOutride/Discord-Deleting-DM

@viasux
Copy link

viasux commented Aug 25, 2019

Doesn't work anymore.

@victornpb
Copy link

victornpb commented Aug 29, 2019

Doesn't work anymore.

EDITED: WORKING AS JAN-2020
Take a look at this https://github.com/victornpb/deleteDiscordMessages
full disclaimer, I'm the author.

@viasux
Copy link

viasux commented Aug 29, 2019

Doesn't work anymore.

WORKING AS AUG-2019
Take a look at this https://github.com/victornpb/deleteDiscordMessages
full disclaimer, I'm the author.

Works! thank you! - just wish I could delete all messages in all channels :P

@gtkiller422
Copy link

Doesn't work anymore.

WORKING AS AUG-2019
Take a look at this https://github.com/victornpb/deleteDiscordMessages
full disclaimer, I'm the author.

didnt work man

@victornpb
Copy link

WORKING AS AUG-2019
Take a look at this https://github.com/victornpb/deleteDiscordMessages
full disclaimer, I'm the author.

didnt work man

It is working as JAN-2020

@SmilingStallman
Copy link

Works for me still on Chromium, but not firefox. Works on most channels, that is. For one channel I do get

authorID="ommited" guildId="618621882110967809" channelId="663328918719168514" afterMessageId="" beforeMessageId="" hasLink=false hasFile=false
Error searching messages, API responded with status 403!
{"message":"Missing Access","code":50001}

Despite being able to hand delete messages in that channel aslo

@jaytareen
Copy link

I was able to delete only my messages from the private text message. How can I delete the other person message. Please advice

@ayoubkhan558-zz
Copy link

I found this repo pretty useful. Its fast and easy to use.

https://github.com/victornpb/deleteDiscordMessages

@DakotaIrsik
Copy link

Senior Software Developer here. Nice work, thanks for saving me some time!

@jsoctocat
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment