The anti delete feature is one of the most loved features in WhatsApp bots. When someone deletes a message in a group, the bot can still show what was deleted. Many users think this is magic, but in reality, it is smart message tracking using Baileys events.
When a message is received, Baileys gives your bot full access to the message object. If you store that message data, you can reuse it later even if the sender deletes it from WhatsApp.
let store = {};
sock.ev.on('messages.upsert', ({ messages }) => {
const msg = messages[0];
store[msg.key.id] = msg;
});
Baileys provides a protocol message event when a message is deleted.
sock.ev.on('messages.update', (updates) => {
updates.forEach(update => {
if(update.update?.message?.protocolMessage?.type === 0){
const deletedId = update.key.id;
const originalMsg = store[deletedId];
if(originalMsg){
sock.sendMessage(update.key.remoteJid, {
text: "Deleted Message:\n" + (originalMsg.message.conversation || "Media Message")
});
}
}
});
});
For images, videos, and stickers, you can download and save media when first received, then resend it if deleted.
The anti delete feature is simple logic with powerful impact. By storing messages and listening to deletion events, your bot can reveal deleted content easily using Baileys.