Back

[SOLVED] Delete file from bucket after some time

  • 0
  • Storage
Hemish11
19 Jul, 2023, 16:05

I want to delete files in my bucket which are more than 1 day old. So how can I do so? I thought to write a function to delete files and use cron to execute it, but by doing that I wouldn't be able to delete files after 1 day is completed. So how can I do that?

TL;DR
To delete files from a bucket after a certain time period: 1. Use the hidden attribute called $createdAt to check the creation date and time of the file. This attribute is available in the File List model. 2. Implement logic in your function to delete only files created within the desired time period (e.g., within the last 24 hours). This can be done by utilizing timestamps in your database. 3. To schedule the execution of your function, use Cron expressions. You can learn more about Cron expressions and how to use them in the Appwrite documentation. 4. Create a scheduled function in the Appwrite dashboard to handle the deletion
D5
19 Jul, 2023, 16:48

You will need a scheduled function. You can easily schedule them from dashboard

D5
19 Jul, 2023, 16:49

In setting tabs, you have the option

D5
19 Jul, 2023, 16:50

You will need to express it using Cron expressions

D5
19 Jul, 2023, 16:50
Hemish11
19 Jul, 2023, 17:15

The issue is that lets say i created a file at 10 hrs in day and the cron is scheduled to run at 12 hrs, so now that file will only stay for 2 hr, and I want it to stay for 1 entire day. The also issue is that lets say file is created at 11 50 and cron will then delete at 12

D5
19 Jul, 2023, 17:16

You could add logic in the function to delete only files created just 24h (database things include timestamps)

Hemish11
19 Jul, 2023, 17:17

So how can i get the time of file creation?

D5
19 Jul, 2023, 17:29

Like any other attribute but with $createdAt as attribute

Hemish11
19 Jul, 2023, 17:36

Sorry but cant get you

D5
19 Jul, 2023, 17:39

You have a hidden attribute called $createdAt https://appwrite.io/docs/models/fileList

D5
19 Jul, 2023, 17:39

This attribute has the creation date, hour, etc.

D5
19 Jul, 2023, 17:42

So you can check with such attribute when it was created and delete if it has been created during the desired period

Hemish11
19 Jul, 2023, 17:43

Got it, thanks a lot

Hemish11
19 Jul, 2023, 17:43

[SOLVED] Delete file from bucket after some time

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