Discord bot slash command showing "Application did not respond" error when using bulk message deletion

I have a Discord bot that utilizes slash commands, and I’m encountering an issue with one particular command. When I execute my bulk delete command, it appropriately removes the messages I want, but afterward, Discord displays “The application did not respond” instead of my success confirmation.

My imports:

import discord
from discord import app_commands
from discord.ext import commands
from asyncio import sleep

The command code:

@tree.command(
    name="cleanup",
    description="Removes a specific amount of messages from current channel."
)
async def cleanup(ctx, amount: int):
    try:
        if ctx.user.guild_permissions.manage_messages:
            await ctx.channel.purge(limit=amount + 1)
            await sleep(2)
            await ctx.response.send_message(f"Successfully removed {amount} messages!", ephemeral=True)

        else:
            await ctx.response.send_message("You need manage messages permission to use this.", ephemeral=True)
    except discord.errors.NotFound:
        await ctx.response.send_message("Something went wrong processing your request. Try again.", ephemeral=True)
    except discord.errors.Forbidden:
        await ctx.response.send_message("Bot lacks permissions for this action.", ephemeral=True)
    except Exception as error:
        await ctx.response.send_message(f"Unexpected error happened: {str(error)}", ephemeral=True)

It’s strange because this issue arises only with this particular command. All my other slash commands function seamlessly. The bot accurately deletes the designated number of messages but subsequently shows the error instead of my confirmation message. I even tried introducing a 2-second delay, suspecting it might be a timing issue, but that didn’t solve the problem.

Error from console:

2024-04-09 00:22:43 ERROR    discord.app_commands.tree Ignoring exception in command 'cleanup'
Traceback (most recent call last):
  File "/home/container/commands.py", line 27, in cleanup
    await ctx.response.send_message(f"Successfully removed {amount} messages!", ephemeral=True)
discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction

What could cause Discord to lose track of the interaction after executing the purge command? This situation is quite frustrating since the command works, but users perceive it as a failure.

Yeah, the timeout issue is real. Managing edge cases manually turns into a nightmare - you’re constantly juggling defer calls, followup messages, and token lifetimes across commands.

I hit the same problems and switched to automating my Discord bot responses through Latenode. Set up a workflow that handles the interaction response instantly, then runs the bulk deletion as a separate background task. Discord gets its acknowledgment right away, and your cleanup runs reliably without timing out.

With Latenode you can build proper error handling and retry logic without messy bot code. I’ve added logging for deleted message counts to spreadsheets and admin notifications for large purges.

The webhook integration is clean - bot triggers the workflow, Latenode handles all the timing stuff.

Discord has a 3-second timeout for interactions, and your purge is taking too long. When you delete a bunch of messages, the API calls go over that limit and Discord kills your interaction token before you can respond. You’re doing this backwards - respond first, then delete. Just add await ctx.response.defer(ephemeral=True) at the very start of your command, then use await ctx.followup.send() for your final message instead of ctx.response.send_message(). The defer basically tells Discord ‘hey, I got this but need more time’ so it won’t timeout. I’ve been using this fix in production bots for bulk mod commands and it works perfectly.

Your issue is the interaction token gets invalidated when purge deletes the original slash command message. You’re doing limit=amount + 1, which accidentally nukes the interaction message and breaks the response chain. Change it to just limit=amount instead of adding that extra +1. That’ll keep Discord’s interaction alive so you can send your confirmation properly.

You’re hitting Discord’s timeout because bulk purging takes longer than the 3-second limit. The interaction token becomes invalid before you can send a confirmation. Here’s the fix: immediately defer your response with await ctx.response.defer(ephemeral=True), then do the purge. After that, use await ctx.followup.send() for your confirmation message. This tells Discord you’re handling the interaction and prevents timeouts.

Others nailed the timeout diagnosis, but here’s a different angle. Skip the defer/respond dance and use webhooks instead. Set up a webhook for the channel, send your confirmation message right away, then run the purge. Webhooks let you send follow-up messages even after interactions expire. I’ve used this for big moderation tasks that take 10+ seconds. Users get instant feedback, you can still share detailed results later, and Discord’s timeouts become irrelevant.

The problem is that purge operations take longer than Discord’s interaction window, so your token expires before you can send confirmation. Don’t defer - just respond immediately and handle deletion after. Move your response to the start: await ctx.response.send_message(f'Deleting {amount} messages...', ephemeral=True) then do the purge. If you want confirmation, use await ctx.edit_original_response(content=f'Successfully removed {amount} messages!'). This gives users instant feedback that you got their command while deletion runs in the background. That 2-second sleep you added actually makes it worse - it just delays your response to Discord’s interaction system even more.