- Why should I use spatie/laravel-model-cleanup instead of Laravel’s built-in prunable trait?
- This package was designed for Laravel 5.8–8.x, where prunable didn’t exist. If you’re on Laravel 9+, prunable is the superior choice—it offers dry-run, logging, and better scheduling. Only use this package if you’re stuck on older Laravel versions or need a quick legacy solution.
- How do I configure cleanup rules for a model?
- Add the `GetsCleanedUp` trait to your model and define a `cleanUp()` method. Use the `CleanupConfig` object to set rules like `olderThanDays(5)` or `where('status', 'inactive')`. The package provides methods for time-based, conditional, and custom cleanup logic.
- Does this package support soft deletes?
- No, it doesn’t. The package deletes records permanently. If you need soft deletes, use Laravel’s native `SoftDeletes` trait alongside prunable (for Laravel 9+), which handles both soft and hard deletes with more flexibility.
- Can I run cleanup asynchronously or in batches?
- The package includes a `clean:models` Artisan command for batch processing, but it’s not async by default. For high-volume databases, consider extending it to use Laravel queues or chunking queries manually to avoid locking issues.
- Will this package work with Laravel 9 or 10?
- Officially, it supports Laravel 5.8–8.x. While it *might* work on newer versions, it’s unmaintained and lacks compatibility guarantees. Laravel 9+ includes prunable, which is the recommended alternative for modern apps.
- How do I test cleanup logic in PHPUnit?
- Mock the `cleanUp()` method and the `clean:models` command. Use Laravel’s `Artisan::call()` to trigger cleanup and assert database changes. Test edge cases like empty results or foreign key constraints to ensure safety.
- Are there security risks with this package?
- Since it’s unmaintained, there’s a potential for SQL injection if cleanup queries aren’t properly sanitized. Always validate inputs in your `cleanUp()` method and avoid dynamic SQL. For production, prefer prunable or manually implemented cleanup.
- Can I schedule cleanup tasks with Laravel’s task scheduler?
- Yes, but you’ll need to wrap the `clean:models` command in a scheduled job. For Laravel 9+, prunable integrates natively with `schedule:run`, offering more features like dry-runs and logging without extra setup.
- Does this package handle foreign key constraints during deletion?
- No, it doesn’t. If your models have foreign key constraints, deletions may fail. Use `onDelete('cascade')` in migrations or manually handle related records in your `cleanUp()` method to avoid errors.
- What’s the migration path from spatie/laravel-model-cleanup to prunable?
- Replace the `GetsCleanedUp` trait with `Prunable`. Replicate cleanup logic using prunable’s `prunable()` method (e.g., `prunable()->olderThan(5)->delete()`). Update your scheduler to use `schedule:run` instead of the `clean:models` command.