Discord.py how to delete all roles in server

Question:

I’m trying to delete all roles in my discord server, but it takes a huge amount of time. So I decided to do this task with discord.py bot, but I’m getting this error:

discord.errors.HTTPException: 400 Bad Request (error code: 50028): Invalid Role

Here’s my code:

@client.command()
async def delroles(ctx):
 for role in ctx.guild.roles:  
     await role.delete()
Asked By: Vasiliy Peshko

||

Answers:

The problem is that all users have an "invisible role" called @everyone, which is impossible to remove.

Do:

async def delroles(ctx):
 for role in ctx.guild.roles:  
     try:  
        await role.delete()
     except:
        await ctx.send(f"Cannot delete {role.name}")
Answered By: Andrei
import discord
from discord.ext import commands

client = commands.Bot(command_prefix='>')

@client.event
async def on_ready():
    print("Log : "+str(client.user))

@client.command()
async def delete(ctx):
    for role in ctx.guild.roles:
        try:
            await role.delete()
        except:
            pass


client.run("token")
Answered By: tre flse
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.