Delete superuser in django

Created at 31-May-2023 , By samar

Deleting a superuser in Django involves accessing the Django shell and using Python code to delete the superuser.

PS E:\laragon\www\playground\djangolearn> python manage.py shell
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> superuser = User.objects.get(username='samar')
>>> superuser.delete()
(5, {'admin.LogEntry': 4, 'auth.User': 1})
>>>

Here is step by step guide to delete the superuser from django application.

1 Open a Python shell by running the following command in your terminal

python manage.py shell

2 Once you are in the Python shell, import the User model

from django.contrib.auth.models import User

3 Find the superuser you want to delete by querying the User model

superuser = User.objects.get(username='<username>')

Replace with the actual username of the superuser you want to delete.

4 Delete the superuser

superuser.delete()

Please note that this method will directly delete the superuser from the database without any confirmation prompt. Make sure to double-check the username before running the delete command.

After deleting the superuser, you may need to create a new superuser using the createsuperuser command if you still need administrative access to the Django admin.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.