Clear or drop or delete table from the code, is it possible?
The docs only gave the solution for one document, what about multiple documents or the whole table?
3 Replies
you can delete multiple documents in a mutation with a loop, like
for (const id of ids) { await ctx.db.delete(id); }
or await Promise.all(ids.map((id) => ctx.db.delete(id)))
.
if you want to delete all documents in a table that is too big to do in a single mutation, you can do it with the migration pattern described in https://stack.convex.dev/migrating-data-with-mutations#big-tables
on the other hand, deleting a table can only be done in the dashboard, not from code. while deleting a table is faster than deleting its rows and can happen atomically, it's more complicated because it affects indexes and schema defined on the table.Migrating Data With Mutations
Using mutations to migrate data in Convex.
What about deleting everything in the database? I just want to have a clean slate while developing
We're considering how to make this exact case easier -- clearing a deployment is useful while developing. For now your options are to delete all of the rows with migrations, or go through and delete all tables in the dashboard, or delete the whole project in the dashboard (including your prod deployment, so that might not be an option). For the first option, there's an example at https://github.com/get-convex/ai-town/blob/main/convex/testing.ts in the function
debugClearAll
GitHub
ai-town/convex/testing.ts at main · get-convex/ai-town
A MIT-licensed, deployable starter kit for building and customizing your own version of AI town - a virtual town where AI characters live, chat and socialize. - get-convex/ai-town