Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix: limt media deletion batch size to avoid pg bind limits (#13072)

Valery Meleshkind003a9c3f413
diff --git a/packages/shared/src/server/media-deletion.ts b/packages/shared/src/server/media-deletion.ts
index eefa590f90..f781676862 100644
--- a/packages/shared/src/server/media-deletion.ts
+++ b/packages/shared/src/server/media-deletion.ts
@@ -1,5 +1,9 @@
+import chunk from "lodash/chunk";
+
import { prisma } from "../db";
+const BATCH_SIZE = 10_000;
+
interface MediaFileRef {
id: string;
bucketPath: string;
@@ -55,16 +59,20 @@ export async function deleteMediaFiles(params: {
return 0;
}
- // Delete from S3 first
- await storageClient.deleteFiles(mediaFiles.map((f) => f.bucketPath));
-
- // Delete from PostgreSQL (cascades to traceMedia/observationMedia)
- await prisma.media.deleteMany({
- where: {
- id: { in: mediaFiles.map((f) => f.id) },
- projectId,
- },
- });
+ // Process in batches to stay under PostgreSQL's 32,767 bind variable limit.
+ // S3 is deleted before PG per batch to avoid orphaned storage files.
+ // All callers target expired or soft-deleted media with retry semantics,
+ // so partial failure self-heals on retry (S3 deletes are idempotent).
+ const chunks = chunk(mediaFiles, BATCH_SIZE);
+ for (const batch of chunks) {
+ await storageClient.deleteFiles(batch.map((f) => f.bucketPath));
+ await prisma.media.deleteMany({
+ where: {
+ id: { in: batch.map((f) => f.id) },
+ projectId,
+ },
+ });
+ }
return mediaFiles.length;
}