Last modified: Sep 22, 2026

Delete SQLite Database in Python

Delete SQLite Database in Python

Deleting an SQLite database in Python is straightforward. You can remove the database file directly or drop all tables inside it. This guide explains both methods with clear examples.

Method 1: Delete the Database File

Use Python’s os.remove() function to delete the SQLite database file. This removes the entire database permanently.


import os

# Path to the SQLite database file
db_file = "example.db"

# Check if the file exists
if os.path.exists(db_file):
    os.remove(db_file)
    print(f"{db_file} has been deleted.")
else:
    print(f"{db_file} does not exist.")

example.db has been deleted.

Method 2: Drop All Tables

If you want to keep the database file but remove all data, use the sqlite3 module to drop tables.


import sqlite3

# Connect to the SQLite database
conn = sqlite3.connect("example.db")
cursor = conn.cursor()

# Get list of all tables
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()

# Drop each table
for table in tables:
    table_name = table[0]
    cursor.execute(f"DROP TABLE {table_name};")
    print(f"Table {table_name} dropped.")

# Commit changes and close connection
conn.commit()
conn.close()

Table users dropped.
Table orders dropped.

Delete Database Safely

Always check if the database file exists before deleting. This prevents errors and ensures safe execution.


import os

db_file = "example.db"

# Safely delete the database file
if os.path.exists(db_file):
    os.remove(db_file)
    print("Database deleted successfully.")
else:
    print("Database file not found.")

Database deleted successfully.

Important Notes

Deleting a database is irreversible. Back up important data before proceeding. Also, ensure no active connections exist to the database file.

Conclusion

Deleting an SQLite database in Python is simple using either os.remove() or SQL commands like DROP TABLE. Choose the method that best fits your needs. For more database operations, check out our guide on Python SQLite Example: Simple Database Operations.