delete(); return; } // Otherwise, we get the actual IDs that should be deleted… $allIDs = static::select($field)->get()->pluck($field)->all(); $whereInIDs = array_diff($allIDs, $values); // …and see if we can delete them instead. if (count($whereInIDs) < $maxChunkSize) { static::whereIn($field, $whereInIDs)->delete(); return; } // If that's not possible (i.e. this array has more than maxChunkSize elements, too) // then we'll delete chunk by chunk. static::deleteByChunk($values, $field, $maxChunkSize); } public static function deleteByChunk(array $values, string $field = 'id', int $chunkSize = 65535): void { DB::transaction(static function () use ($values, $field, $chunkSize): void { foreach (array_chunk($values, $chunkSize) as $chunk) { static::whereIn($field, $chunk)->delete(); } }); } }