Best way to keep only last NNN rows of a table

How about something like (completely untested):

from anvil.tables import app_tables, in_transaction

MAX_ROWS = 1000


@in_transaction
def make_db_housekeeping():
    rows = app_tables.my_table.search()
    for row in rows[:len(rows) - MAX_ROWS]:
        row.delete()
1 Like