simcofS
Convex Community4mo ago
5 replies
simcof

what is the fastest way to clear a table with 1m+ rows

our dataset is relatively small, most tables are 1-3m records. we needed to make a change to schema that required clearing all data in a few existing tables. using the dashboard clear table function is very slow.

claude suggested a batch process which is also very slow:
export const clearTableBatch = mutation({
args: { batchSize: v.optional(v.number()) },
handler: async (ctx, { batchSize = 100 }) => {
const documents = await ctx.db
.query("your_table_name")
.take(batchSize);

for (const doc of documents) {
await ctx.db.delete(doc._id);
}

return {
deleted: documents.length,
hasMore: documents.length === batchSize
};
},
});

what is the recommended way?
Was this page helpful?