selectRaw('id, uri, count(uri) as occurences') ->whereNotNull('uri') ->groupBy('uri') ->orderBy('created_at') ->having('occurences', '>', 1) ->chunk(50, function($statuses) { foreach($statuses as $status) { $this->info("Found duplicate: $status->uri"); Status::whereUri($status->uri) ->where('id', '!=', $status->id) ->get() ->map(function($status) { $this->info("Deleting Duplicate ID: $status->id"); StatusDelete::dispatch($status); }); } }); } }