A Laravel package to permanently delete soft-deleted or expired records based on a defined "cleanable" query scope or after a configurable cutoff date.
- PHP 8.2+
- Laravel 9, 10, 11, or 12
composer require myparcelcom/resource-cleanupUpdate Laravel's package auto-discovery cache
php artisan package:discoverPublish the config file:
php artisan vendor:publish --tag=resource-cleanup-configconfig/resource-cleanup.php:
return [
// Records older than this many days will be deleted (default: 90)
'default_retention_days' => env('RESOURCE_CLEANUP_RETENTION_DAYS', 90),
// Valid models to clean up
'models' => [
\App\Models\Order::class,
\App\Models\AuditLog::class,
],
];Run cleanup for all models defined in the config:
php artisan resource-cleanup:runTarget specific models from the config:
php artisan resource-cleanup:run --model=App\\Models\\OrderPreview what would be deleted without deleting anything:
php artisan resource-cleanup:run --dry-runImplement the CleanableResource contract on any model to define its own retention period:
use Illuminate\Database\Eloquent\Builder;
use MyParcelCom\ResourceCleanup\Contracts\CleanableResource;
class AuditLog extends Model implements CleanableResource
{
public static function scopeCleanable(): Builder
{
// narrow down the query scope by adding where() clauses
return self::query();
}
}Build the container and run the test suite:
docker compose run --rm appRun a specific test or filter:
docker compose run --rm app vendor/bin/phpunit --filter test_it_deletes_soft_deleted_recordsDrop into a shell inside the container:
docker compose run --rm app bashRun Composer commands without installing PHP locally:
docker compose run --rm app composer require some/packagecomposer install
composer test