How To Reset Django Database

Haris Bin Nasir Avatar

·

·

When working with databases, we frequently find ourselves in circumstances where we must completely reset the database. The database may have been loaded with too much useless data as a result of the addition or removal of some database tables, changes in the database design, logic and relationship concerns, or the database was populated with too much worthless data. Whatever the situation may be, Django makes dealing with this issue a breeze.

Furthermore, Django includes a few commands that can take care of this for us. We’re going to show you how to use one of these commands to reset the database in Django.

Reset SQLite3 Database

If you need to reset the SQLite3 Database for your Django project, follow the instructions below.

  • Delete the db.sqlite3 database file. If this file contains sensitive information, you should create a backup.
  • Delete the migrations folder from each Django application.
  • Using python manage, create migrations for all Django apps. makemigrations is a python command. In some cases, migrations for the apps are not made; in this case, add the application names to this command, such as this python manage. makemigrations MyAppOne, MyAppTwo, MyAppThree, MyAppFour, MyApp MyAppThree.
  • Lastly, migrate the migrations using this command: python manage.py migrate.

Reset the Whole Database in Django

If we need to totally reset the database, we’ll run the following command: (Note: If you apply this code, it will also erase all existing superusers.

Copied!
python manage.py flush

Reset an App Database Tables in Django

The command below will be used to remove the database tables of a Django application. All migrations for that application are reversed with the following code.

Copied!
python manage.py migrate MyApp zero

Conclusion

I hope you found this tutorial on how to reset a database for your Django project useful. As always, If you have found this tutorial useful do not forget to share it and leave a comment if you have any questions. Happy Coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *