Delete CMS input after the date of the event

Question:
I’m wondering if there is a way for me to delete a CMS after a date.

Product:
I’m doing this on Wix Studio.

What are you trying to achieve:
Essentially, Im making a Cheer Competition Tracker. These competitions are input using a form on the website that connected to a CSM dataset. Part of the data we track is the date the event. After that the date passes I want to event to no longer be in the system/deleted.

What have you already tried:
I looked through the setting and haven’t found anything.

I’m not sure what your coding proficiency is, but you can run a scheduled job daily or hourly. Essentially you would need to write a function that queries your database then delete from there. As an example:

async function removeOldEvents () {
  let current_time = new Date()
  let old_events = await wixData.query("Events").lt("eventDate", current_time).find();
  if (old_events.items.length === 0) { 
      return "No old events to remove"
  }
  let events_to_remove = [];
  for (let i = 0; i < old_events.items.length; i++ ) { 
      events_to_remove.push(old_events.items[i]._id) 
  }
  let remove_response = await wixData.bulkRemove("Events", events_to_remove);
  return remove_response;
}

Note: before using a function like the above, I would test it by returning the results of the query first to make sure it is working as intended so that you don’t remove valuable data. Secondly, I think it might be a better solution to save the old events in a separate database in case you want to access them later instead of deleting the data entirely. The code for that would be:

async function removeOldEvents () {
  let current_time = new Date()
  let old_events = await wixData.query("Events").lt("eventDate", current_time).find();
  if (old_events.items.length === 0) { 
      return "No old events to remove"
  }
  let events_to_remove = [];
  let events_to_migrate = [];
  for (let i = 0; i < old_events.items.length; i++ ) { 
      events_to_remove.push(old_events.items[i]._id); 
      events_to_migrate.push(old_events.items[i]); 
  }
  let migrate_response = await wixData.bulkSave("OldEvents", events_to_migrate);
  let remove_response = await wixData.bulkRemove("Events", events_to_remove);
  return { 
      "migrateResponse": migrate_response
      "removeResponse": remove_response
  }
}

That second solution will remove the old events from your active database AFTER saving the data to a new database collection.

Hope that helps!

1 Like