This episode is for members only

Sign up to access "Laravel Performance" right now.

Get started
Already a member? Sign in to continue
Playing
15. Mass deleting

Transcript

00:00
We're now going to take a look at mass deleting.
00:02
This is something that I've come across a few times, and it's not entirely obvious how you can just clear out a database table. There are a few ways to do this, and I'm
00:11
going to show you the fastest way that we can get this working. So to get this working, we're going to go ahead and create another console command.
00:18
It's just a little bit easier from the command line. So let's go ahead and make our command called delete posts, and let's head over to that delete posts command. We'll update the signature here to be posts,
00:30
and let's just call this flush, or let's just call this delete. So we want to go ahead and, in this command, just clear out the entire posts table. Bear in mind, we have 200,000 here.
00:41
So the key thing to think about here is we don't want to iterate over anything to delete them. We just want to send one query to clear out this whole thing. So let's go ahead and run this command, php artisan post
00:52
delete. Of course, it's not doing anything at the moment. Now, your first instinct might be to say post and then delete. This doesn't actually work.
01:01
There's no method on our model called delete. And if we try and run this, sure enough, we'll get an error here. It doesn't exist, and it cannot be called statically. Now, what I've done in the past is said, well,
01:13
if I can't do this, then what I'm going to have to do is go ahead and grab all of the posts and then go ahead and delete them, something like that. Now, I'm not going to run this because we already
01:22
know that using post get on a huge amount of records is going to take a long, long time. 200,000 records are going to have to be pulled into memory. Then we're going to have to delete them.
01:31
It's going to take a huge amount of time. So the next best thing in terms of this, you might be thinking, well, we could chunk these records. So we could pull in a batch of 1,000.
01:42
We could go ahead and create a closure in here for each of these posts. And we could then say posts delete, for example. Or we could say posts each and delete
01:52
whatever we wanted to do. Now, there's a much easier way. And like I said at the start of the episode, it's not entirely obvious.
01:59
What we can actually do is we can say post. We can pull in a fresh query builder. And then we can say delete. It's as simple as that.
02:07
Query will return to us a new build instance. And that's now not on the model. It's a query builder. So we can just call delete.
02:14
So we're going to go ahead and run this in here. And that should be pretty quick. And if we come back over to posts, sure enough, they are all cleared out.
02:21
All of our users are still in here. But all of our posts are gone. And it was very, very fast. So for things like this where we need to potentially update
02:30
multiple things as well, you want to pull in your query builder first and then go ahead and call that query. That's just going to clear out everything for you.
15 episodes1 hr 9 mins

Overview

Let's keep our Laravel applications feeling snappy! In this course, we cover the absolute fundamentals you need to keep in mind when building anything with Laravel.

While Laravel handles a lot for you, it's easy to fall into the trap of not considering and monitoring performance as you go. Keep these tips in your toolbelt, and you'll be able to develop faster apps, from the beginning.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!