Automatically delete an item based on condition (Cronjob like functionality)

Describe the issue:

Hi, I have a model called Schedule Module, and it contains a Date field called “Start Date”.

I want to run an automatic function everyday that checks whether the “Start Date” is earlier than current date i.e. “today”. If yes, then delete the Schedule Module item.

Is this possible?

Hi @itsahmedkamal,

We don’t have a “delete record at X time” function built in, but we do have Scheduled Unpublishing. Would that suffice? You can schedule any record to publish and/or unpublish at certain dates and times.

If that’s not quite enough, you can also use the DatoCMS Content Management API combined with an external script (on a cron job or similar) to do this via API calls. Example logic:

  1. Get the current date
  2. List records matching your criteria, e.g. using the filter object to filter by date. Make sure you account for any potential time zone differences!
  3. Save their record IDs in an array.
  4. Bulk destroy those records by ID (be very careful and make sure you have your filters right!!). If you don’t need to destroy them outright, bulk unpublish might be safer.

If you don’t have infrastructure set up to host a script on a cron job, something like a Cloudflare Worker on a cron trigger should be able to do the job.

Hope that helps!

1 Like

Yes, that helps a lot. Thank you for clarifying everything.

1 Like

Hi @roger,

Can we bulk destroy/unpublish the blocks instead of model records?

My use case is that I have block named Scheduled Course and it references to multiple Schedule Modules i.e. there could be multiple modules under the Scheduled Course block.

And I’m utilising the “delete record at X time” functionality suggested by you on Schedule Modules.

Now, I want that when a module gets unpublished - the block containing this module should be deleted.

Is this possible?


I’ve looked into the Validatons tab and played with these options:

  1. When a publishing is requested and this field references some unpublished records
  2. When unpublishing is requested for a record referenced by this field
  3. When deletion is requested for a record referenced by this field

but it doesn’t work the way according to my use case.

Hey @itsahmedkamal,

Sorry for the slow reply; it was a US holiday last week.

It’s a bit unclear to me here, but does Scheduled Course have to be a block instead of a traditional model? If you make it a model instead of a block and use the Links field type to link it to Scheduled Modules, then the unpublish validations should work the way you expect. Blocks don’t really have their own pub/unpub status; they only live “inside” of its parent record. However, if you use a real model and link to other real models, you should be able to control their publishing more easily.

If if I’m misunderstanding the use case, could you please provide a link to your project so I can take a look? If privacy is a concern, you can email us at support@datocms.com (and cc me at r.tuan@datocms.com). Thank you!

Based on my understanding, I think it makes more sense to make Scheduled Course a block instead of a model.

However, I have sent my project link in the email and also CCed you.

Thanks!

1 Like