Using PosgreSQL, to empty a database, I used this code :
def delete_tables_and_indexes(engine):
""" Delete all tableis and indexes """
if engine.name == 'postgresql':
# Drop tables
sql_raw_query = 'select \'drop table if exists "\' || tablename || \'" cascade;\' from pg_tables where schemaname=\'public\';'
for result in engine.execute(sql_raw_query):
engine.execute(result[0])
# Drop types
sql_raw_query = 'select \'drop type if exists "\' || t.typname || \'";\' '\
'FROM pg_type t LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace '\
'WHERE (t.typrelid = 0 OR (SELECT c.relkind = \'c\' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) '\
'AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid) '\
'AND n.nspname NOT IN (\'pg_catalog\', \'information_schema\');'
for result in engine.execute(sql_raw_query):
engine.execute(result[0])
This use some raw SQL query, to drop things in a way postgresql understand it, unlinking all schema dependencies.
With SQLite, which does not handle those king of dependencies in a "hard" way, you can easily use :
metadata.drop_all(engine)