In this episode, we take things a step further by automating the pruning process we've just set up. Rather than running php artisan model:prune
manually (and risking forgetting, or wasting time SSHing into your server every time), we look at how to schedule this pruning so it runs automatically.
We'll hop into Laravel's console kernel and set up scheduled commands to run pruning at regular intervals—maybe daily, weekly, or even just for testing, every minute. We see how to use artisan's schedule:run
command and understand why you'll likely want to fine-tune the frequency for your own app.
We also discuss how to prune different models on different schedules. For example, one model might be pruned weekly, while another only gets pruned on weekends. You'll learn how to pass arguments and options to your scheduled commands, allowing you to be more specific in what gets cleaned up and when.
Finally, once everything is scheduled, all that's left is to set up a cron job on your server to kick off the Laravel scheduler. That means your application's data stays tidy and you never have to think about manual pruning again!