PHP code example of spatie / laravel-queued-db-cleanup

1. Go to this page and download the library: Download spatie/laravel-queued-db-cleanup library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / laravel-queued-db-cleanup example snippets


Spatie\LaravelQueuedDbCleanup\CleanDatabaseJobFactory::new()
    ->query(YourModel::query()->where('created_at', '<',  now()->subMonth()))
    ->deleteChunkSize(1000)
    ->dispatch();

return [
    /*
     * To make sure there's only one job of a particular cleanup running,
     * this package uses a lock. Here, you can configure the default
     * store to be used by the lock and the release time.
     */
    'lock' => [
        'cache_store' => 'redis',

        'release_lock_after_seconds' => 60 * 20
    ],

    /*
     * The class name of the job that will clean that database.
     *
     * This should be `Spatie\LaravelQueuedDbCleanup\Jobs\CleanDatabaseJob`
     * or a class that extends it.
     */
    'clean_database_job_class' => Spatie\LaravelQueuedDbCleanup\Jobs\CleanDatabaseJob::class,

    /*
     * In order to handle deadlocks on a high traffic table, the package can
     * automatically retry the transaction that performs the delete query
     * a specified number of times
     */
    'delete_query_attempts' => 3,
];

Spatie\LaravelQueuedDbCleanup\CleanDatabaseJobFactory::new()
    ->query(YourModel::query()->where('created_at', '<',  now()->subMonth()))
    ->deleteChunkSize(1000)
    ->dispatch();

Spatie\LaravelQueuedDbCleanup\CleanDatabaseJobFactory::new()
    ->query(YourModel::query()->where('created_at', '<',  now()->subMonth()))
    ->deleteChunkSize(1000)
    ->getBatch()
    ->onConnection('redis')
    ->onQueue('cleanups')
    ->dispatch();

Spatie\LaravelQueuedDbCleanup\CleanDatabaseJobFactory::new()
    ->query(YourModel::query())
    ->onDatabaseConnection('other_connection')
    ->deleteChunkSize(1000)
    ->dispatch();

CleanDatabaseJobFactory::forQuery(YourModel::query())
    ->deleteChunkSize(10)
    ->stopWhen(function (Spatie\LaravelQueuedDbCleanup\CleanConfig $config) {
        return $config->pass === 3;
    })
    ->dispatch();

$batch = CleanDatabaseJobFactory::forQuery(YourModel::query())
    ->deleteChunkSize(10)
    ->getBatch();

// you could store this batch id somewhere
$batchId = $batch->id;

$batch->dispatch();

\Illuminate\Support\Facades\Bus::findBatch($batchId)->cancel();
bash
php artisan vendor:publish --provider="Spatie\LaravelQueuedDbCleanup\LaravelQueuedDbCleanupServiceProvider" --tag="config"