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?
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:
- Get the current date
- 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!
- Save their record IDs in an array.
- 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!
Yes, that helps a lot. Thank you for clarifying everything.
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:
- When a publishing is requested and this field references some unpublished records
- When unpublishing is requested for a record referenced by this field
- When deletion is requested for a record referenced by this field
but it doesn’t work the way according to my use case.
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 firstname.lastname@example.org (and cc me at email@example.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.