Dropping Indexes
Indexes should evolve with query patterns.
If an index is unused or redundant, dropping it can reduce write overhead and storage.
Core Concepts
flowchart TD
A[Identify candidate index] --> B[Validate it's safe to remove]
B --> C[DROP INDEX]
C --> D[Verify with PRAGMA index_list]
| Command | Effect |
|---|---|
DROP INDEX idx_name; | Removes named index |
DROP INDEX IF EXISTS idx_name; | Safe no-error drop if missing |
Code Examples
-- Review current indexes on orders_comp.
PRAGMA index_list(orders_comp);
| seq | name | unique | origin | partial |
|---|---|---|---|---|
| e.g. 0 | idx_orders_customer_date | 0 | c | 0 |
-- Drop an index safely.
DROP INDEX IF EXISTS idx_orders_customer_date;
| Expected output |
|---|
| Index removed (or no error if it did not exist). |
-- Verify the index list after drop.
PRAGMA index_list(orders_comp);
| Expected output |
|---|
| Dropped index no longer appears. |
SQLite-Specific Nuances
SQLite Nuance
You cannot drop implicit primary-key structure with DROP INDEX.
DROP INDEX targets explicit index objects and relevant autoindexes, not table PK definitions.
Common Pitfalls / Best Practices
Pitfall
Dropping indexes without checking critical query paths, causing sudden read slowdowns.
Best Practice
Track why each index exists; if a rationale is unclear and usage is low, test removal in staging first.
Quick Challenge
Write a defensive statement to drop idx_users_name only if it exists.
View Solution
DROP INDEX IF EXISTS idx_users_name;
| Expected output |
|---|
| Safe index removal with no error when absent. |