#expiring entry
4 messages · Page 1 of 1 (latest)
Hi @sleek sphinx. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Yes, it is possible to have a document in Convex that will auto-delete after a specific timeframe. You can achieve this by scheduling a function (typically a mutation) to run at a future time, which will delete the document. Convex provides a scheduler that allows you to use methods like runAfter or runAt to schedule such deletions.
Here’s a simplified example:
import { mutation, internalMutation } from "./_generated/server";
import { internal } from "./_generated/api";
import { v } from "convex/values";
export const createExpiringDoc = mutation({
args: { data: v.string(), expiresInMs: v.number() },
handler: async (ctx, args) => {
const id = await ctx.db.insert("myTable", { data: args.data });
await ctx.scheduler.runAfter(args.expiresInMs, internal.myTable.deleteDoc, {
docId: id,
});
},
});
export const deleteDoc = internalMutation({
args: { docId: v.id("myTable") },
handler: async (ctx, args) => {
await ctx.db.delete(args.docId);
},
});
In this example, when you create a document, you also schedule its deletion after a specified number of milliseconds. This approach is resilient and does not require you to maintain any external infrastructure. Scheduled functions are stored in the database and will run even if there is downtime or restarts in your system. You can find more details in the Convex scheduling documentation.