Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Model Cleanup

Laravel Model Cleanup Laravel Package

spatie/laravel-model-cleanup

Deprecated: use Laravel’s built-in Prunable. Spatie’s laravel-model-cleanup deletes unneeded Eloquent records via a cleanUp() configuration per model, and an artisan command to prune records older than a given age or matching custom rules.

View on GitHub
Deep Wiki
Context7

Clean up unneeded records

Frequently asked questions about Laravel Model Cleanup
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport