whereNotIn($field, $values)->delete(); return; } $allIds = static::query()->select($field)->get()->pluck($field)->all(); $deletableIds = array_diff($allIds, $values); if (count($deletableIds) < $maxChunkSize) { static::query()->whereIn($field, $deletableIds)->delete(); return; } static::deleteByChunk($deletableIds, $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::query()->whereIn($field, $chunk)->delete(); } }); } }