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

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 😅

Maybe something like this
- <:nodejs:637383194580090882> Create a
node
function - <:functions:1108648038156746792> Add this code to the function.
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);
});
}
- <:realtime:1108648060894068776> Set the function to be triggered every midnight in your
appwrite.json
in theschedule
property.
{
"$id" : "someFunctionID",
"name" : "function",
"runtime" : "node-16.0",
"path" : "functions/function",
"entrypoint": "src/index.js",
"events" : [],
"schedule" : "0 0 * * *",
"timeout" : 15
}
- <:appwritefire:823999000330895380> Deploy it
- <:appwrite:637383039499894787> Sit back and let Appwrite do the work for you from now on, Any day at server midnight. 🕛

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😅

I'm currently hosting on a droplet on DigitalOcean

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

That solution can always work 👍
As for your concerns
- There is no rate limit when executing function using an API key,
- 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.
- 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: �*
- 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.

Why not choosing the scheduled-function?

Oh I will do this

Thanks for the help btw

This way it set-n-forget

cache is another big concern

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

ya much less risky when reading data


What you are saying is completely valid. Even I try to use SDKs wherever possible. But here are my points
- 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
- That’s totally fine, I am not worried about time, as these use cases are executed as backend jobs.
- I totally get that, and this is the place where user needs to be aware of their schema and relations.
- 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

- Agreed! and in the future Appwrite is probably going to support much more batch actions toward the database. 2-3. 👍
- 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.

Yeah exactly
Recommended threads
- Migrate data from one DB to another via ...
Hello, I have a database that is currently used in production with lots of data in it. I would like to create a dev database with the same content to test out ...
- Upgrade from 1.6.2 to 1.7.2 error during...
I just went through the process of upgrading my selfhosted instance from `1.6.2` to `1.7.2`: ``` docker run -it --rm \ --volume /var/run/docker.sock:/var/r...
- Search queries
do any of yall know how to properly search for single words in a name of a document? for example if the name is `Tape PP-Solvent 50mm/100m 28μm White`, I want ...
