Home > Software engineering >  How to delete records of multiple tables in one go? Sqlite
How to delete records of multiple tables in one go? Sqlite

Time:12-09

For a project i need to to delete all data from all tables in a database,

I tried

DELETE FROM table1,table2,table3,...;

But it doesnt work. Any advice ? Thank

CodePudding user response:

I would like to refer You to this related post

How do I use cascade delete with SQL Server?

as You will find there several possible solutions.

When using SQL it means that Your data is relations, which means most of the records are somehow related in the different tables and this relation is expressed with foreign keys. However when attempting to delete data which is id is related with data in another table a cascade deletion should be implemented, the other way around it is add additional boolean column named isDeleted(as example ofcourse) and just alter specific rows to true in this specific column and then filter by preferences. Hopefully I have managed somehow to provide with alternative and/or possible solution to Your problem.

Leaving also this link which gives some examples on cascade deletion and guide on how to implement it. ->

https://www.sqlshack.com/delete-cascade-and-update-cascade-in-sql-server-foreign-key/

P.S. also if You want just to DELETE all the data You can either use TRUNCATE TABLE or DROP DATABASE query. With the latest option You will have to recreate the database once more.

CodePudding user response:

Because you want to delete all databases from all tables, you are essentially deleting the database.

The sqlite way of doing this is to just delete the database file.

In contrast to postgres or mongodb or most databases, sqlite doesn't have a server so no need to call DROP DATABASE.

As soon as the file is gone and you open the database from your application you will be in a blank state again.

  • Related