2

I saw this question where it is presented a command to drop all tables from an sqlite database. Is it possible to add exceptions, i.e., drop all tables except table X Y and Z?

Thanks

EDIT I also saw that it is possible in SQL. Is this script translatable to sqlite?

Community
  • 1
  • 1
Camandros
  • 449
  • 6
  • 22

1 Answers1

4
PRAGMA writable_schema = 1;
DELETE FROM sqlite_master WHERE type = 'table' AND name NOT IN ('X', 'Y', 'Z');
PRAGMA writable_schema = 0;
VACUUM;

Or, to get the DDL:

SELECT 'DROP TABLE ' || name || ';' FROM sqlite_master
    WHERE type = 'table' AND name NOT IN ('X', 'Y', 'Z');
Matthias Wiehl
  • 1,799
  • 16
  • 22