How do I auto delete collection data?

I am trying to auto delete collection data after it gets older than 5 days. I used a tutorial by wix but some of their language has changed since then. It doesn’t seem to be working. There are no errors, but nothing is happening.

This is what have for the jsw file

import wixData from 'wix-data';

export function multiply(factor1, factor2) {
 return factor1 * factor2;
}

export function clearOldData() {
 return wixData.query("College").find().then(result => {
 let expirationDate = new Date();
        expirationDate.setDate(expirationDate.getDate()-5);
 let oldListings = wixData.result.items.filter(item => item._updatedDate < expirationDate);
        oldListings.forEach(item => {
            wixData.remove("College", item._id);
        })
    })
}

I am not quite sure if wixData.result.items.filter(item => item._updatedDate < expirationDate); is right.

This is what I have in the job scheduler. Does the time I put means that it runs this job daily?

// /backend/jobs.config
{
 "jobs": [ // Define up to 20 different jobs
 // Choose one of the options below to define the job execution interval:
 // {
 //   // // Option 1 - define execution interval using a cron expression
 //   // "functionLocation": "/Autodelete.clearOldData", // Relatively to Backend folder, started by slash
 //   // "functionName": "clearOldData",
 //   // "description": "describe your job", // Optional
 //   // "executionConfig": {
 //   //   "cronExpression": "0 8 * * *" // Set intervals of up to 1 hour 
 //   //   // Read about Cron expressions: [https://en.wikipedia.org/wiki/Cron#CRON_expression]
 //   }
 // },
    {
 // Option 2 - define execution interval by setting time, day of week and day of month
 "functionLocation": "/module/Autodelete.jsw", // Relatively to Backend folder, started by slash
 "functionName": "clearOldData",
 "description": "describe your job",, // Optional
 "executionConfig": {
 "time": "07:59", 
 
 // "hh:mm" 24h format, UTC timezone (e.g. 13:00) 
 // Optional - uncomment section below for more complex intervals
 //"dayOfWeek": "Monday", // Day of week 
 //"dateInMonth": 1 // Number between 1-31 
      }
    },
    {
 // Add more jobs. Up to 20 jobs supported.
    }
  ]
}

1 . it should be

let oldListings = result.items.filter(item => item._updatedDate < expirationDate);

without wixData
2. in the jobs.config file you have to delete all the comments otherwise it’s not going to work. It’s a JSON, not JavaScript code and you can’t comment inside a json.
3. in the jobs.config you have some extra commas. You can’t have a trailing comma in a json so remove them.

example for commas that bring to an error (in red):

"description": "describe your job",, 
 "executionConfig": {
 "time": "07:59", 
      }

I tried your tactics and it still doesn’t seem to be working.

So now my code looks like this for the .jsw

export function clearOldData() {
 return wixData.query("AR-COLLEGE").find().then(result => {
 let expirationDate = new Date();
        console.log("expiration date set");
        expirationDate.setDate(expirationDate.getDate()-10);
 let oldListings = result.items.filter(item => item._updatedDate < expirationDate);
        oldListings.forEach(item => {
            wixData.remove("AR-COLLEGE", item._id);
            console.log("deleted");
        })
    })
}

This is what my job.config looks like. Is the /module/Autodelete.jsw correct?

{
 "jobs": [ 
    {

 "functionLocation": "/module/Autodelete.jsw", 
 "functionName": "clearOldData",
 "description": "describe your job",
 "executionConfig": {
 "time": "16:13"
      }
    },
    {

    }
  ]
}

@covid19schooltracker

  1. make sure the path to your function is right.

  2. get rid of the red lines :

{
 "jobs": [ 
    {
 "functionLocation": "/module/Autodelete.jsw", 
 "functionName": "clearOldData",
 "description": "describe your job",
 "executionConfig": {
 "time": "16:13"
      }
    },
    {

    }
  ]
}
  1. Connect your website to google Stackdriver (dashboard > settings > site monitoring) and see if it logs any error related to this function.

@jonatandor35 Thank you so much for your speedy response! For the path to my function, should I add /module or not?

@covid19schooltracker only if you put the Autodelete.jsw file inside a folder named “module”.