Removal of items from a collection after a given time interval

Hey I’m trying to create expiring feature for inserted items.
I’m using afterInsert data hook and setTimeout :

import { fetch } from 'wix-fetch';
import wixData from 'wix-data';
const sleep = m => new Promise(r => setTimeout(r, m))

export async function Hookz_afterInsert(item, context) {
console.log("item inserted")
console.log(item)

await sleep(1000 * 30)

let removed = await wixData.remove("Hookz", item._id)
console.log(removed)

return item
}

However, I’m getting an ExecutionTimeoutError error:

save operation failed: ExecutionTimeoutError: WebMethod request timed-out after 14 seconds... Did you forget to resolve a promise?

It appears that backend functions have a limit of 14 seconds timeout.

Is there another way to implement this functionality ?

Thanks

You should look at using Wix Job Scheduler for this.
https://www.wix.com/corvid/forum/corvid-tips-and-updates/video-how-to-execute-timed-tasks-with-the-job-scheduler
https://support.wix.com/en/article/corvid-scheduling-recurring-jobs

@givemeawhisky I thought about using it, however the scheduler works like a cron job- static time .
I’m looking for a more dynamic method, remove the item 24hrs from insert.

@edwardfrogz
Okay, yes I understand your way of thinking and if you search the forum for previous posts there are posts that give you code samples where the user captures the created date for example and then does something with it after a set time.

@givemeawhisky I couldn’t find that post you were referring to, could you kindly send the url to the relevant discussion ?

I will have a look and report back later, sleep time for me. :sleeping:
Unless somebody beats me to it and replies back to before then… :crossed_fingers: