Download the PHP package spatie/laravel-queued-db-cleanup without Composer

On this page you can find all versions of the php package spatie/laravel-queued-db-cleanup. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-queued-db-cleanup

Safely delete large numbers of records

Latest Version on Packagist Tests Total Downloads

Deleting many database records in one go using Laravel has a few pitfalls you need to be aware of:

The pitfalls are described in more detail in this post on the Flare blog.

This package offers a solution to safely delete many records in large tables. Here's an example:

The code above will dispatch a cleanup job that will delete the first 1000 records that are selected by the query. When it detects that 1000 records have been deleted, it will conclude that possibly not all records are deleted and it will redispatch itself.

We'll also make sure that this cleanup job never overlaps. This way the number of database connections is kept low. It also allows you the schedule this cleanup job repeatedly through CRON without having to check for an existing cleanup process.

By keeping the chunk size small, the query executes faster and potential table locks will not be held for long periods of time. The cleanup job will also finish fast, so you won't hit an execution time limit.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

The package uses a lock to prevent multiple deletions for the same query to be executed at the same time. We recommend using Redis to store the lock.

Behind the scenes this package leverages job batches. Make sure you have created the batches table mentioned in the Laravel documentation.

Optionally, you can publish the config file with:

This is the contents of the published config file:

Usage

This code will dispatch a cleanup job that will delete the first 1000 records that are selected by the query. When it detects that 1000 records have been deleted, it will conclude that possibly not all records are deleted and it will redispatch itself.

The job will not redispatch itself when there were fewer records deleted than the number given to deleteChunkSize.

Starting the cleanup in a scheduled tasks

It is safe to start the cleanup process from within a scheduled task. Internally the package will use a lock to make sure no two cleanups using the same query are running at the same time.

If a scheduled task starts a cleanup process while another one is still running, the new cleanup process will be cancelled.

Customizing the queue and connection name

Internally, the package uses job batches. Using getBatch you can get the batch and call methods like onConnection and onQueue on it. Don't forget to dispatch the batch at the end, by calling dispatch().

Customizing the database connection

Using onDatabaseConnection will allow you to delete records on another connection.

Manually stopping the cleanup process

By default, the cleanup jobs will not redispatch themselves anymore when they detect that they've deleted fewer records than the chunk size. You can customize this behaviour by calling stopWhen. It should receive a closure. If the closure returns true the cleanup will stop.

stopWhen receives an instance of Spatie\LaravelQueuedDbCleanup\CleanConfig. It contains these properties to determine whether the cleanup should be stopped:

Using the batch to stop the cleanup process

You can use the batch id to stop the cleanup process

Somewhere else in your codebase you could retrieve the stored batch id and use it to cancel the batch, stopping the cleanup process.

Events

You can listen for these events. They all have one public property, cleanConfig, which is an instance of Spatie\LaravelQueuedDbCleanup\CleanConfig.

Spatie\LaravelQueuedDbCleanup\Events\CleanDatabasePassStarting

Fired when a new pass starts in the cleanup process.

Spatie\LaravelQueuedDbCleanup\Events\CleanDatabasePassCompleted

Fired when a pass has been completed in the cleanup process.

Spatie\LaravelQueuedDbCleanup\Events\CleanDatabaseCompleted

Fired when the entire cleanup process has been completed.

Testing

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-queued-db-cleanup with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
doctrine/dbal Version ^3.0
illuminate/contracts Version ^10.0|^11.0
laravel/serializable-closure Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package spatie/laravel-queued-db-cleanup contains the following files

Loading the files please wait ....