HOWTO · SQLite
How to Delete a Column in SQLite Database
Learn how to remove a column from an SQLite table, check dependencies, and preserve the remaining schema and data.
On this page
Removing a column deletes that column’s data. Make a backup before changing a database, and check the table’s dependent schema objects before choosing a migration. SQLite provides a direct ALTER TABLE ... DROP COLUMN operation in supported versions; rebuilding the table is the fallback when the operation is unavailable or unsuitable.
Drop a Column from an SQLite Table
The direct approach is concise, but it is appropriate only when the SQLite library that will run the migration supports DROP COLUMN and the column is not required by a constraint or another schema object. A table rebuild gives you control over the replacement schema and data mapping, at the cost of more migration steps. In either case, test against a copy or a transactionally protected database and verify the result before relying on it.
Check SQLite Version and Column Dependencies
Direct ALTER TABLE ... DROP COLUMN support starts with SQLite 3.35.0. Check the SQLite library used by the application or command-line tool that will execute the migration; different installations can use different libraries. The SQLite ALTER TABLE documentation gives the version boundary and the restrictions: a drop can fail when the column is part of a primary key or unique constraint, an index, a partial-index predicate, a foreign key, a CHECK constraint, a generated-column expression, a trigger, or a view. The example below prints the library version, the table’s columns, and the retained row after the drop. It uses Python’s standard sqlite3 module with an in-memory database, so it does not modify a project database.
Before dropping a column, inspect the table definition and related indexes, triggers, views, generated expressions, and constraints. SQLite can reject a drop when the column is involved in a schema feature that depends on it. Review foreign-key relationships too: a table change can affect the integrity of related data. If the direct operation is rejected, identify the dependency and decide whether it should be updated or whether a rebuild is the safer migration.
Drop a Column with ALTER TABLE
The supported form is ALTER TABLE table_name DROP COLUMN column_name;. The shorter form ALTER TABLE table_name DROP column_name; is not the SQLite syntax. The following self-contained example creates a small table, removes last_name, then reads the resulting column list and the remaining values. Run this example:
import sqlite3
connection = sqlite3.connect(":memory:")
connection.execute(
"CREATE TABLE students "
"(id INTEGER PRIMARY KEY, first_name TEXT NOT NULL, "
"last_name TEXT NOT NULL, age INTEGER NOT NULL)"
)
connection.execute(
"INSERT INTO students (first_name, last_name, age) VALUES (?, ?, ?)",
("Ada", "Lovelace", 36),
)
print("SQLite", sqlite3.sqlite_version)
connection.execute("ALTER TABLE students DROP COLUMN last_name")
columns = [row[1] for row in connection.execute("PRAGMA table_info(students)")]
print("Columns:", ", ".join(columns))
print("Rows:", connection.execute("SELECT id, first_name, age FROM students").fetchall())
connection.close()
SQLite 3.40.1
Columns: id, first_name, age
Rows: [(1, 'Ada', 36)]
The run reports SQLite 3.40.1, the remaining columns id, first_name, and age, and the retained row (1, 'Ada', 36). The value stored in last_name is discarded; DROP COLUMN does not copy it elsewhere.
Rebuild the Table When Necessary
When the direct operation cannot be used, rebuild the table with a definition that preserves the needed types, primary key, defaults, collations, checks, and foreign-key clauses. The following ordered template removes last_name; adapt the definition and both matching column lists to your table. In the copy step, each destination column must correspond to the source expression in the same position.
- Before the transaction, inspect foreign-key relationships and save the definitions of indexes, triggers, and views that must survive. If the documented rebuild requires disabling foreign-key enforcement, record its current setting and run
PRAGMA foreign_keys = OFF;before starting the transaction; changing this pragma inside a transaction has no effect. Keep enforcement enabled when your schema and rebuild sequence allow it. - Start with
BEGIN IMMEDIATE;, create the replacement with the intended definition—for this example,CREATE TABLE new_students (id INTEGER PRIMARY KEY, first_name TEXT NOT NULL, age INTEGER NOT NULL);—then copy explicitly withINSERT INTO new_students (id, first_name, age) SELECT id, first_name, age FROM students;. - Before touching
students, compareSELECT COUNT(*) FROM students;withSELECT COUNT(*) FROM new_students;and compare the retained key and values with queries over both tables. Also check required values and constraints against your migration’s expectations. If any check differs, runROLLBACK;and investigate; the original table is still intact. - Only after those checks pass, run
DROP TABLE students;andALTER TABLE new_students RENAME TO students;. Recreate the saved indexes and triggers, and update or recreate views and other schema objects that depend on the old definition. - Run
PRAGMA foreign_key_check;and inspect its rows. If it reports any violations, roll back rather than commit. Otherwise runCOMMIT;; if you disabled foreign keys, restore the recorded setting after the transaction (for example,PRAGMA foreign_keys = ON;if it was on originally) and verify it. Keep a backup until the migration and application checks pass.
This is a schema-specific migration template, not a universal copy-and-paste script. In particular, review references from other tables before dropping the original, and follow SQLite’s table rebuild procedure for the foreign-key and dependent-object details that apply to your schema. CREATE TABLE ... AS SELECT is not a substitute when schema properties matter because it does not reproduce the original table definition.
Verify the Schema and Data
After either method, inspect the schema with PRAGMA table_info(students) or query sqlite_schema for the table and its dependent objects. Confirm that last_name no longer appears, retained columns have the intended definitions, and expected rows and values remain. For a rebuild, compare row counts and important values with the source before dropping it, then verify indexes, triggers, views, and foreign-key integrity after the replacement. Keep the backup until these checks pass in the environment where the migration will run.