Skip to content

DELETE with LIMIT and ORDER BY - possible with Drift? #3380

Closed Answered by simolus3
tomekit asked this question in Q&A
Discussion options

You must be logged in to vote

SQLite supports DELETE with LIMIT and ORDER BY

Only with a compile-time option that is not commonly enabled (and also not enabled by sqlite3_flutter_libs).

But you can select the oldest todo items (and apply a limit there) and then use a subquery as a condition to only delete those:

Future<void> deleteOld(int itemsToDelete) async {
  final oldItems = selectOnly(todoItems)
    ..addColumns([todoItems.id])
    ..orderBy([OrderingTerm.asc(todoItems.creationDate)])
    ..limit(itemsToDelete);

  final stmt = todoItems.delete()..where((t) => t.id.isInQuery(oldItems));
  await stmt.go();
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@tomekit
Comment options

Answer selected by tomekit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants