Easily deleting old soft-deleted records with Quicksand
Published on by Diaa Fares
When building applications, there are times when you would like to allow users to remove data from their view but keep the record in the database. An example could be allowing a user to delete their account, but you want to give them the opportunity to restart it later. If all their data is completely removed, then they would have to start completely over.
Laravel’s Eloquent provides this ability out of the box, and when models are soft-deleted, they are not removed from the database. Instead, a deleted_at attribute is stored. The database record remains, and Eloquent ignores it unless explicitly told otherwise by a withTrashed
attribute.
One problem with this is over time your soft-deleted records can fill up your database and take up a lot of space.
Tighten Co. recently released a new package called “Quicksand” which will help you solve this problem by automating and scheduling the deleting of old soft-deleted records.
Quicksand is an Artisan command that you can run through the Laravel scheduler to force the deletion of your soft-deleted records. You can specify which classes you want to clean up, how long the records should remain, and it does the rest. Let’s take a look at this package.
Installation:
First, install Quicksand through Composer:
composer require tightenco/quicksand
Then add Quicksand Service provider in config/app.php:
'providers' => [ ... Tightenco\Quicksand\QuicksandServiceProvider::class,
After that you should publish Quicksand config to edit it as you want, run the following command in your terminal:
php artisan vendor:publish --provider="Tightenco\Quicksand\QuicksandServiceProvider"
Finally, Schedule the following command by adding it in app/Http/Console/Kernel.php:
protected function schedule(Schedule $schedule){ $schedule->command('quicksand:run') ->daily();}
Quicksand Options
Let’s take a look at the available options in Quicksand that are available in the config/quicksand.php file
return [ // Days before deleting soft deleted content 'days' => 30, // Whether to log the number of soft deleted records per model 'log' => false, // List of models to run Quicksand on 'models' => [ // Example::class, // User::class => [ // 'days' => '30' // per-model days setting override // ] ]];
First, you have the option to set the days before permanent removal, an option to turn logging on or off for the number of soft deleted records. Lastly, the most important option is the “models” array where you can add the list of models that you would like Quicksand to do its cleanup process on.
You can add your models simply like this:
User::class,
Or if you want more control, you can override the days for a particular model independently like this:
Post::class => [ 'days' => '60' // per-model days setting override]
If you are curious about how it works, here’s the core of it:
What Quicksand is doing is fetching all the soft-deleted records using the onlyTrashed
method, then limits based on the deleted_at
column when it’s less than the config days and finally force deletes the results.
That’s it, simple options, easy to use, and a very convenient package. If you are looking to automate the cleanup of your soft deleted records give these a try and you can check out the source code of Quicksand at Github
Another package that would make a great pairing with Quicksand is Cascading Soft-Deletes for when you want to delete all associations.