Course contentsShow
← Databases & SQL
Lesson 1132 of 1,95310. Embedded and Lightweight DatabasesFree lesson

Data Pruning and Retention Policies

Automatically manage database size by defining cleanup routines for aged or irrelevant records.

In this lesson, you will learn how to implement automated cleanup routines to keep your embedded database lean and performant by pruning stale data.

When building embedded applications—such as IoT devices or mobile offline-first apps—storage space is a finite and precious resource. If you allow logs, cached sync data, or old user activity to accumulate indefinitely, your database file size will balloon. This leads to slower query performance, increased fragmentation, and potential application crashes due to storage exhaustion.

The Strategy: Time-Based Pruning

The most common approach for managing data growth is a Retention Policy. You define a threshold (e.g., "keep records for only 30 days") and execute a pruning routine to remove any data that exceeds that limit.

In a relational system like SQLite, you can automate this using a simple DELETE statement. To ensure this doesn't impact user experience, you should trigger this routine during idle times or at application startup.

Implementation Example

Consider a sensor_logs table. To keep your database from growing out of control, you can run a cleanup routine that removes any entries older than 30 days:

-- Remove records older than 30 days
DELETE FROM sensor_logs 
WHERE timestamp < datetime('now', '-30 days');

-- Reclaim space in the file
VACUUM;

Critical Considerations

  1. Performance Impact: Running a large DELETE operation can lock your database. If you have millions of rows, consider deleting in smaller, batched chunks to keep the database responsive.
  2. Fragmentation: When you delete data, the database leaves "empty" holes in the file. While SQLite will reuse these slots for future data, running VACUUM occasionally is necessary to shrink the file size on disk and defragment the storage.
  3. Trigger-Based Pruning: While you could use Triggers (Lesson 15) to delete a row whenever a new one is inserted, this can lead to write amplification and slower inserts. It is generally better to perform "batch pruning" at a scheduled interval rather than constant housekeeping.

By establishing a clear retention policy, you ensure your embedded database remains agile, keeping only the information that provides immediate value to the user.

Key Takeaway: Implement scheduled pruning routines to remove stale data, and follow up with a VACUUM command to reclaim disk space and maintain peak database performance.