Easily deleting old soft-deleted records with Quicksand

Published on by

Easily deleting old soft-deleted records with Quicksand image

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.

Diaa Fares photo

Laravel Artisan, Web Developer, Tech Geek

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Version 4 of Tinkerwell is available now. Get the most popular PHP scratchpad with all its new features and simplify your development workflow today.

Visit Tinkerwell
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Generate Code Coverage in Laravel With PCOV image

Generate Code Coverage in Laravel With PCOV

Read article
Non-backed Enums in Database Queries and a withSchedule() bootstrap method in Laravel 11.1 image

Non-backed Enums in Database Queries and a withSchedule() bootstrap method in Laravel 11.1

Read article
Laravel Pint --bail Flag image

Laravel Pint --bail Flag

Read article
Laravel Herd for Windows is now released! image

Laravel Herd for Windows is now released!

Read article
The Laravel Worldwide Meetup is Today image

The Laravel Worldwide Meetup is Today

Read article
Cache Routes with Cloudflare in Laravel image

Cache Routes with Cloudflare in Laravel

Read article