Skip to content

Instantly share code, notes, and snippets.

@dentropy
Created April 13, 2023 03:00
Show Gist options
  • Save dentropy/20ed51ec578365bee5603c5f15dea1ba to your computer and use it in GitHub Desktop.
Save dentropy/20ed51ec578365bee5603c5f15dea1ba to your computer and use it in GitHub Desktop.
SQLAlchemy Delete all tables in database
from sqlalchemy import create_engine, MetaData
import os
# set up the connection to the database
engine_path = "sqlite+pysqlite:///" + "/".join(os.getcwd().split("/")) + "/scraped_data.db"
engine = create_engine(engine_path)
conn = engine.connect()
# get all table names
meta = MetaData()
meta.reflect(bind=engine)
table_names = meta.tables.keys()
# loop through and drop each table
for table_name in table_names:
conn.execute(f"DROP TABLE {table_name}")
# close the connection
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment