Back

Ideas on how the best way to delete a document automatically after x time it has been created?

  • 0
  • Databases
Meli
12 Jul, 2023, 17:43

I need to delete a document after 3 months in x collection. How can I achieve this? Preferably not in some complicated way pls lol 😅

TL;DR
The user wants to automatically delete a document after a certain period of time. Some suggestions include using a scheduled function to delete the document, bypassing Appwrite's database layer and directly running a delete query, or using a frontend admin panel to initiate a function that checks the database and deletes old documents. One user suggests using a Node.js function with a code snippet provided to delete documents older than 3 months. The function can be scheduled to run every midnight. This solution is applicable for self-hosted deployments.
Binyamin
12 Jul, 2023, 17:58

Maybe something like this

  1. <:nodejs:637383194580090882> Create a node function
  2. <:functions:1108648038156746792> Add this code to the function.
TypeScript
const deadlineDate = new Date((+ new Date() - (90 * 24 *60 *60 * 1000) ));

const docs = await databases.listDocuments('[DATABASE_ID]', '[COLLECTION_ID]',[
  Query.lessThan('$createdAt', deadlineDate.toISOString()),
]);

if (docs.total > 0) {
  docs.forEach(async (doc) => {
    const promise = databases.deleteDocument('[DATABASE_ID]', '[COLLECTION_ID]', doc.$id);
  });
}
  1. <:realtime:1108648060894068776> Set the function to be triggered every midnight in your appwrite.json in the schedule property.
TypeScript
{
  "$id"       : "someFunctionID",
  "name"      : "function",
  "runtime"   : "node-16.0",
  "path"      : "functions/function",
  "entrypoint": "src/index.js",
  "events"    : [],
  "schedule"  : "0 0 * * *",
  "timeout"   : 15
}
  1. <:appwritefire:823999000330895380> Deploy it
  2. <:appwrite:637383039499894787> Sit back and let Appwrite do the work for you from now on, Any day at server midnight. 🕛
rohit90314
12 Jul, 2023, 18:48

This may cause some performance and rate limit issue. One another way is to create a direct db connection (bypass appwrite db layer) and just run delete query. PS. This only works on self hosted😅

Meli
12 Jul, 2023, 18:52

I'm currently hosting on a droplet on DigitalOcean

Meli
12 Jul, 2023, 18:54

My original idea of how to do this is to use the frontend (I've made an admin panel for myself that connects to the db) and from there initiate a function that checks the db and delets everything older then 3 months using frontend code. Idk if this is dumb or not lol

Binyamin
12 Jul, 2023, 18:54

That solution can always work 👍

As for your concerns

  1. There is no rate limit when executing function using an API key,
  2. Functions that runs by CRON job, are running async on there own time. and because it's daily it will be no much of a burden on the server.
  3. Probably the most important one. using direct connection for deleting and stuff like this can create some side affect, specially for relation attribute etc. as Appwrite database which based on Utopia-PHP database using a lot of the stuff in some unqiue way. So, only if it's a most it's always best to use the SDK path. Edited: �*
  4. As Steven mentioned, Appwrite utilize cache for Database, and deleting directly can lead to a case when users fetches data that doesn't exists any more which can caused for misinformation and a serious security matter.
Binyamin
12 Jul, 2023, 18:54

Why not choosing the scheduled-function?

Meli
12 Jul, 2023, 18:55

Oh I will do this

Meli
12 Jul, 2023, 18:55

Thanks for the help btw

Binyamin
12 Jul, 2023, 18:55

This way it set-n-forget

Drake
12 Jul, 2023, 21:21

cache is another big concern

Binyamin
12 Jul, 2023, 21:22

Ideally what I ever though about bypassing Utopia is only for retrieving data. so I can get much more data without some limitations.

Drake
12 Jul, 2023, 21:22

ya much less risky when reading data

Drake
12 Jul, 2023, 21:23
rohit90314
13 Jul, 2023, 10:56

What you are saying is completely valid. Even I try to use SDKs wherever possible. But here are my points

  1. Its good that we don’t have rate limits here but I would wanna understand the reasoning behind this. As this is still gonna cause some issue at db layer. Its always recommended to batch db calls instead of making multiple round trips
  2. That’s totally fine, I am not worried about time, as these use cases are executed as backend jobs.
  3. I totally get that, and this is the place where user needs to be aware of their schema and relations.
  4. Yeah I totally agree on this, dirty caches can cause a lot problems. I don’t have a solution for this right now, as I didn’t face any yet. But this definitely has a potential to cause problems. Let me take a dive and see how can this be resolved
Binyamin
13 Jul, 2023, 13:47
  1. Agreed! and in the future Appwrite is probably going to support much more batch actions toward the database. 2-3. 👍
  2. What can be done on this part - it's also behind the back 😉 - direct redis connection, learn the way of the Appwrite keys and delete it or flush all the keys altogether.
rohit90314
13 Jul, 2023, 15:35

Yeah exactly

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more