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
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
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
All Green logo

All Green

All Green is a SaaS test runner that can execute your whole Laravel test suite in mere seconds so that you don't get blocked – you get feedback almost instantly and you can deploy to production very quickly.

All Green
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with VILT and TALL stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

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

Rector

The latest

View all →
Property Hooks Get Closer to Becoming a Reality in PHP 8.4 image

Property Hooks Get Closer to Becoming a Reality in PHP 8.4

Read article
Asserting Exceptions in Laravel Tests image

Asserting Exceptions in Laravel Tests

Read article
Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4 image

Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4

Read article
Basset is an alternative way to load CSS & JS assets image

Basset is an alternative way to load CSS & JS assets

Read article
Integrate Laravel with Stripe Connect Using This Package image

Integrate Laravel with Stripe Connect Using This Package

Read article
The Random package generates cryptographically secure random values image

The Random package generates cryptographically secure random values

Read article