DEV Community

Cover image for How to Delete a Table from a Database in Django
Rohitash Singh
Rohitash Singh

Posted on

How to Delete a Table from a Database in Django

Deleting a single table in Django 3 and above versions can be done efficiently with the following steps:

1. Remove or Comment the Model Class:
In the models.py file, simply remove or comment out the model class corresponding to the table you want to delete. Save the file.

models image

2. Update admin.py, forms.py, and views.py:
Similarly, in the admin.py, forms.py, and views.py files, remove or comment out the model class and all instances where it's being used.
ex..

  • admin.py admin instance example
  • forms.py djngo forms example
  • views.py django views example

3. Run Migrations:
After modifying the files, run the following commands in the terminal to create and apply migrations:

python manage.py makemigrations 
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

By following this streamlined approach, you ensure that all instances of the model class are removed consistently across your project, minimizing the risk of errors and making the deletion process more efficient.

Happy coding!

Top comments (0)