0xtech
0xtech11mo ago

Delete multiple files with id

Hello Team Can I update function to delete multiple files with id ?
import { v } from "convex/values";
import { Id } from "./_generated/dataModel";
import { mutation } from "./_generated/server";

export const deleteById = mutation({
args: {
storageId: v.id("_storage"),
},
handler: async (ctx, args) => {
return await ctx.storage.delete(args.storageId);
},
});
import { v } from "convex/values";
import { Id } from "./_generated/dataModel";
import { mutation } from "./_generated/server";

export const deleteById = mutation({
args: {
storageId: v.id("_storage"),
},
handler: async (ctx, args) => {
return await ctx.storage.delete(args.storageId);
},
});
6 Replies
erquhart
erquhart11mo ago
import { v } from "convex/values";
import { Id } from "./_generated/dataModel";
import { mutation } from "./_generated/server";

export const deleteByIds = mutation({
args: {
storageIds: v.array(v.id("_storage")),
},
handler: async (ctx, args) => {
return await Promise.all(args.storageIds.map(id => ctx.storage.delete(id)));
},
});
import { v } from "convex/values";
import { Id } from "./_generated/dataModel";
import { mutation } from "./_generated/server";

export const deleteByIds = mutation({
args: {
storageIds: v.array(v.id("_storage")),
},
handler: async (ctx, args) => {
return await Promise.all(args.storageIds.map(id => ctx.storage.delete(id)));
},
});
Khalil
Khalil11mo ago
is this performant for 1000s of records?
erquhart
erquhart11mo ago
I wouldn't pass in a list of ids for more than a small number of items - probably works fine into the hundreds, just not a normal pattern and a lot to send over the wire. More typically you'll want to use an indexed query to retrieve the items you want to delete. For thousands of records you'll probably want to paginate and break up the operation into multiple mutations, but I don't know where the boundary is honestly. In my experience low thousands of write operations risks timing out the function, but things are faster now so that may have changed. cc/ @Lee in case you have any rough numbers that would be relevant
lee
lee11mo ago
i would limit it at about 1000. no hard numbers though
Khalil
Khalil11mo ago
Since convex is not relational, I assume there is no equivalent for "on delete: cascade"?
erquhart
erquhart11mo ago
Convex is definitely relational! It just doesn't have certain relational behaviors found in traditional dbs and ORMs built in to its core, instead it provides low level functionality so things like cascading deletes can happen in the software layer. Ents is one good example of that, a library by a Convex team member that implements a lot of typical relational behavior for Convex, including cascading deletes: https://labs.convex.dev/convex-ents/schema/deletes
Cascading Deletes - Convex Ents
Relations, default values, unique fields and more for Convex

Did you find this page helpful?