# Buggy code
async def mass_purge(messages: List[discord.Message], channel: discord.TextChannel):
    """Bulk delete messages from a channel.

    If more than 100 messages are supplied, the bot will delete 100 messages at
    a time, sleeping between each action.

    Note
    ----
    Messages must not be older than 14 days, and the bot must not be a user
    account.

    Parameters
    ----------
    messages : `list` of `discord.Message`
        The messages to bulk delete.
    channel : discord.TextChannel
        The channel to delete messages from.

    Raises
    ------
    discord.Forbidden
        You do not have proper permissions to delete the messages or you’re not
        using a bot account.
    discord.HTTPException
        Deleting the messages failed.

    """
    while messages:
        if len(messages) > 1:
            await channel.delete_messages(messages[:100])
            messages = messages[100:]
        else:
            await messages[0].delete()
            messages = []
        await asyncio.sleep(1.5)


# Traceback (discord.ext.commands.errors.CommandInvokeError)
Traceback (most recent call last):
File "/home/bb8/redenv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 79, in wrapped
ret = await coro(*args, **kwargs)
File "/home/bb8/redenv/lib/python3.8/site-packages/redbot/cogs/cleanup/cleanup.py", line 379, in messages
await mass_purge(to_delete, channel)
File "/home/bb8/redenv/lib/python3.8/site-packages/redbot/core/utils/mod.py", line 45, in mass_purge
await messages[0].delete()
File "/home/bb8/redenv/lib/python3.8/site-packages/discord/message.py", line 700, in delete
await self._state.http.delete_message(self.channel.id, self.id)
File "/home/bb8/redenv/lib/python3.8/site-packages/discord/http.py", line 220, in request
raise NotFound(r, data)
discord.errors.NotFound: 404 NOT FOUND (error code: 10008): Unknown Message

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/bb8/redenv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 863, in invoke
await ctx.command.invoke(ctx)
File "/home/bb8/redenv/lib/python3.8/site-packages/redbot/core/commands/commands.py", line 587, in invoke
await super().invoke(ctx)
File "/home/bb8/redenv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1158, in invoke
await ctx.invoked_subcommand.invoke(ctx)
File "/home/bb8/redenv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 728, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/home/bb8/redenv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 88, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 NOT FOUND (error code: 10008): Unknown Message

# Fixed code
async def mass_purge(messages: List[discord.Message], channel: discord.TextChannel):
    """Bulk delete messages from a channel.

    If more than 100 messages are supplied, the bot will delete 100 messages at
    a time, sleeping between each action.

    Note
    ----
    Messages must not be older than 14 days, and the bot must not be a user
    account.

    Parameters
    ----------
    messages : `list` of `discord.Message`
        The messages to bulk delete.
    channel : discord.TextChannel
        The channel to delete messages from.

    Raises
    ------
    discord.Forbidden
        You do not have proper permissions to delete the messages or you’re not
        using a bot account.
    discord.HTTPException
        Deleting the messages failed.

    """
    while messages:
        # discord.NotFound can be raised when `len(messages) == 1` and the message does not exist.
        # As a result of this obscure behavior, this error needs to be caught just in case.
        try:
            await channel.delete_messages(messages[:100])
        except discord.errors.HTTPException:
            pass
        messages = messages[100:]
        await asyncio.sleep(1.5)