2

What is the proper way to delete entries in certain cases in headless-cms Strapi? If one content type gets a specific value.

For example if the date is reached/expired. In my case I've created an events calendar. It has some content types like title, location and date. Each event has to disappear automatically after the event date is reached. How can I achieve this?

2 Answers2

1

I suggest you to user CRON task. This feature is available in Strapi

Here is the documentation https://strapi.io/documentation/3.0.0-beta.x/configurations/configurations.html#cron-tasks.

So create your CRON task by following the documentation then, in the function, you will have to fetch data that have the date lower than the CRON function execution date.

To do so you can use Query function.

// Fetch data that have the `yourDateAttributeName_lt` lower than the now.
const data = await strapi.query('article').find({
  yourDateAttributeName_lt: new Date()
});

// Delete all entries fetched.
data.forEach((entry) => strapi.query('article').delete({
  id: entry.id
}));
Jim LAURIE
  • 3,859
  • 1
  • 11
  • 14
  • Nice suggestion, but it's better to add "_limit" param, otherwise it will delete default limit items (by default it's 100), so add something like { yourDateAttributeName_lt: new Date(), _limit: 100000, } – Ruslan Korkin Sep 22 '21 at 12:25
0

I advise reading about custom queries https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#custom-queries

For example, with knex delete could be easy as:

await strapi.connections.default('emails').where('isSend', true).del();

For periodic tasks see how to use CRON https://strapi.io/documentation/developer-docs/latest/guides/scheduled-publication.html