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.

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

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

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
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant 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
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
Rector logo

Rector

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

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
A Guide to Pagination in Laravel image

A Guide to Pagination in Laravel

Read article
Laravel Roundup - November image

Laravel Roundup - November

Read article
Bento Laravel SDK image

Bento Laravel SDK

Read article
Laravel 11.30 Released image

Laravel 11.30 Released

Read article
Bluesky notification channel for Laravel image

Bluesky notification channel for Laravel

Read article
Creating a CLI Application With Laravel and Docker image

Creating a CLI Application With Laravel and Docker

Read article