Skip to content

Instantly share code, notes, and snippets.

@orion-v

orion-v/how-to.md

Last active Apr 12, 2021
Embed
What would you like to do?
Delete all messages of an user in a Discord channel or server

Delete discord user message history

This script allow for user specific message deletion from an entire server or a single channel using the browser console. This script uses discord search API and it will only delete messages of a chosen user.

How to use

1. Enable developer mode in discord

Go to user settings > appearance in discord and enable Developer mode.

2. Copy and paste the script to a file so you can change the server and author ids.

3. Replace the server and author ids with your own.

Open discord and right click on the server icon and click copy id. Replace the server id in the script with your server id. Do the same process for the author id by right clicking the avatar image.

4. Run script in console

Press F12 in Chrome or Firefox to open the console. Paste the modified script in the console and press enter.

The more messages the longer it takes. You can check if the messages have been deleted by using the search.


Notes

I think there are some channels that this script won't work with. I think they may be NSFW channels but I haven't tested enough.

Use this script at your own risk

This script was based on the following scripts https://gist.github.com/niahoo/c99284a8908cd33d59b4aff802179e9b#gistcomment-2397287 https://gist.github.com/IMcPwn/0c838a6248772c6fea1339ddad503cce

async function clearMessages() {
const server = "000000000000000000"; // server id number
const author = "000000000000000000"; // user id number
const channel = window.location.href.split('/').pop(); // remove this line to delete all messages of an user from a server
const authToken = document.body.appendChild(document.createElement`iframe`).contentWindow.localStorage.token.replace(/"/g, "");
const headers = { 'Authorization': authToken, 'Content-Type': 'application/json' };
const baseURL = `https://discordapp.com/api/v6/channels`;
let searchURL = `https://discordapp.com/api/v6/guilds/${server}/messages/search?author_id=${author}`;
if (typeof channel !== 'undefined') searchURL = searchURL + `&channel_id=${channel}`;
let clock = 0;
let interval = 500;
function delay(duration) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), duration);
});
}
const response = await fetch(searchURL, {headers});
const json = await response.json();
console.log("There are " + json.total_results + " messages left to delete.");
await Array.from(json.messages).map(message => {
message.forEach(async function(item) {
if(item.hit == true) {
await delay(clock += interval);
await fetch(`${baseURL}/${item.channel_id}/messages/${item.id}`, { headers, method: 'DELETE' });
}
});
});
if (json.total_results > 0) {
delay(clock += interval).then(() => { clearMessages(); });
} else {
console.log("Finished deleting messages")
};
}
clearMessages();
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jul 13, 2018

How exactly can this work in a DM?

Open discord and right click on the server icon and click copy id. Replace the server id in the script with your server id. Do the same process for the author id by right clicking the avatar image.

@Nicckonator

This comment has been minimized.

Copy link

@Nicckonator Nicckonator commented Jul 21, 2018

Sadly doesnt work for me for a DM (not a server/channel)

Uncaught (in promise) TypeError: Cannot convert undefined or null to object VM280:24
at Function.from (native)
at clearMessages (:24:14)

is what i get as error.

any fix?

@TrunerReisar

This comment has been minimized.

Copy link

@TrunerReisar TrunerReisar commented Aug 5, 2018

I tried using this (and the IMcPwn one which worked like a charm up to this point, but that page is gone now) and all I'm getting is this error now:

Uncaught (in promise) TypeError: Cannot read property 'replace' of undefined
at clearMessages (:5:110)
at :37:1

Is this something easily fixable?

@etch286

This comment has been minimized.

Copy link

@etch286 etch286 commented Aug 7, 2018

same as above

@Alvin388

This comment has been minimized.

Copy link

@Alvin388 Alvin388 commented Aug 10, 2018

Not working anymore

@instinctualjealousy

This comment has been minimized.

Copy link

@instinctualjealousy instinctualjealousy commented Sep 21, 2018

If it's anything like the other script, the AuthToken isn't being correctly grabbed and has to be manually inserted instead.

const authToken = "AuthTokenHere";

You can grab it from the dev tools "Application -> Local Storage -> https://discordapp.com", under "token" after a page refresh, I think.

Be sure to remove line #5 if you're trying to do what I'm doing, otherwise it'll say there's 0 messages to delete. It seems to be working for me so far.

@Knuckl3s

This comment has been minimized.

Copy link

@Knuckl3s Knuckl3s commented Sep 29, 2018

Hello
Can someone give a complete code to delete private messages? It works very well on servers, but for private discussions I have no idea what to change.

Thank you

@PointMeAtTheDawn

This comment has been minimized.

Copy link

@PointMeAtTheDawn PointMeAtTheDawn commented May 23, 2019

My guess is it is using https://discord.js.org/#/docs/main/stable/class/GuildMember, which doesn't exist in a DM. You'd have to repoint all references to GuildMembers to https://discord.js.org/#/docs/main/stable/class/User (and they don't have stuff like Nickname or DisplayName, just Username IIRC).

@GreenReaper

This comment has been minimized.

Copy link

@GreenReaper GreenReaper commented Dec 9, 2019

You probably need &include_nsfw=true at the end of the channel string to get NSFW channels. That's what the search bar uses.

However right this moment it won't work because the search API as a whole appears to have been removed, breaking Discord's own search bar. This is presumably because it was causing the recent downtime (search is easy to abuse, causing a denial-of-service deliberately or not).

@orion-v

This comment has been minimized.

Copy link
Owner Author

@orion-v orion-v commented Dec 10, 2019

You probably need &include_nsfw=true at the end of the channel string to get NSFW channels. That's what the search bar uses.

Thanks for that info.

However right this moment it won't work because the search API as a whole appears to have been removed, breaking Discord's own search bar. This is presumably because it was causing the recent downtime (search is easy to abuse, causing a denial-of-service deliberately or not).

Yours was the first comment that githubgist alerted me. I didn't know there were so many comments here. I will see if I can answer some questions when I have some more time.

Discord search appears to be working again. I don't know if they changed anything or if the code will still work.

@GreenReaper

This comment has been minimized.

Copy link

@GreenReaper GreenReaper commented Dec 18, 2019

It works, but the script gets rate limited at an interval of 500ms. Even 3000ms was too much after a while. The 429 response has this body:

{
  "global": false, 
  "message": "You are being rate limited.", 
  "retry_after": 26104
}

The value appears to be in ms. Typically further requests (at least of that type) are refused with a decreasing timeout from 30 seconds to 0.

@swordfite

This comment has been minimized.

Copy link

@swordfite swordfite commented Apr 22, 2020

This script by a-SynKronus works well in DMs. But make sure you keep the dms of the person you want your dms deleted in open.


clearMessages= function() {
const author = "YOUR_ID_HERE";
const authToken = "YOUR_TOKEN_HERE";
const channel = window.location.href.split('/').pop();
const headers = { 'Authorization': authToken, 'Content-Type': 'application/json' };

let clock = 0;
let interval = 500;
function delay(duration) {
	return new Promise((resolve, reject) => {
		setTimeout(() => resolve(), duration);
	});
}

fetch(`https://discordapp.com/api/v6/channels/${channel}/messages/search?author_id=${author}`, {headers})
	.then(response => response.json())
	.then(json => {
		Array.from(json.messages).map(message => {
			message.forEach(function(item) {
				if(item.hit == true) {
					delay(clock += interval).then(() => { fetch(`https://discordapp.com/api/v6/channels/${item.channel_id}/messages/${item.id}`, { headers, method: 'PATCH', body: JSON.stringify({'content': 'This comment has been overwritten.'}) }) });
					delay(clock += interval).then(() => { fetch(`https://discordapp.com/api/v6/channels/${item.channel_id}/messages/${item.id}`, { headers, method: 'DELETE' }) });
				}
			});
		});

		if (json.total_results > 0) { delay(clock += interval).then(() => { clearMessages(); }); }
	});

}
clearMessages();

@marcosrocha85

This comment has been minimized.

Copy link

@marcosrocha85 marcosrocha85 commented Nov 17, 2020

Uncaught (in promise) TypeError: document.body.appendChild(...).contentWindow.localStorage.token is undefined
When trying to use the script to delete all messages from myself in all channels.

I think Discord does not save token on localStorage anymore. Now we should use OAuth2 in order to get Authorization token.


Quick workaround: Follow this.

@scsmash3r

This comment has been minimized.

Copy link

@scsmash3r scsmash3r commented Apr 12, 2021

Works well, but often catches up the rate limits. Thanks to @marcosrocha85 for the token tip.

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