Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Batch deleteLinks in chunks of 100 and drop unused bulkDeleteLinks.

Kiran K7ea588df7f17
diff --git a/apps/web/lib/api/links/bulk-delete-links.ts b/apps/web/lib/api/links/bulk-delete-links.ts
deleted file mode 100644
index eaafc7a7f6..0000000000
--- a/apps/web/lib/api/links/bulk-delete-links.ts
+++ /dev/null
@@ -1,37 +0,0 @@
-import { prisma } from "@/lib/prisma";
-import { storage } from "@/lib/storage";
-import { recordLink } from "@/lib/tinybird";
-import { R2_URL } from "@dub/utils";
-import { linkCache } from "./cache";
-import { ExpandedLink } from "./utils";
-
-export async function bulkDeleteLinks(links: ExpandedLink[]) {
- if (links.length === 0) {
- return;
- }
-
- return await Promise.all([
- // Delete the links from Redis
- linkCache.deleteMany(links),
-
- // Record the links deletion in Tinybird
- recordLink(links, { deleted: true }),
-
- // For links that have an image, delete the image from R2
- links
- .filter((link) => link.image?.startsWith(`${R2_URL}/images/${link.id}`))
- .map((link) =>
- storage.delete({ key: link.image!.replace(`${R2_URL}/`, "") }),
- ),
-
- // Update totalLinks for the workspace
- prisma.project.update({
- where: {
- id: links[0].projectId!,
- },
- data: {
- totalLinks: { decrement: links.length },
- },
- }),
- ]);
-}
diff --git a/apps/web/lib/api/links/delete-links.ts b/apps/web/lib/api/links/delete-links.ts
index b63a82f7d9..3a3ac1ba53 100644
--- a/apps/web/lib/api/links/delete-links.ts
+++ b/apps/web/lib/api/links/delete-links.ts
@@ -2,16 +2,20 @@ import { deleteDiscountCodes } from "@/lib/discounts/delete-discount-code";
import { prisma } from "@/lib/prisma";
import { storage } from "@/lib/storage";
import { recordLink } from "@/lib/tinybird";
-import { R2_URL } from "@dub/utils";
+import { chunk, R2_URL } from "@dub/utils";
import { Prisma } from "@prisma/client";
import { linkCache } from "./cache";
import { ExpandedLink } from "./utils";
+const DELETE_LINKS_BATCH_SIZE = 100;
+
/**
* Canonical bulk link deletion:
* 1. Delete related DiscountCodes (and enqueue provider cleanup)
* 2. Delete Link rows + decrement totalLinks (transaction)
* 3. Run side effects (Redis / Tinybird / R2)
+ *
+ * Processes links in batches of 100.
*/
export async function deleteLinks(
links: ExpandedLink[],
@@ -20,9 +24,36 @@ export async function deleteLinks(
},
): Promise<{ deletedCount: number }> {
if (links.length === 0) {
- return { deletedCount: 0 };
+ return {
+ deletedCount: 0,
+ };
+ }
+
+ let deletedCount = 0;
+
+ const batches = chunk(links, DELETE_LINKS_BATCH_SIZE);
+
+ for (const [batchIndex, batch] of batches.entries()) {
+ const batchDeletedCount = await deleteLinksBatch(batch, options);
+
+ deletedCount += batchDeletedCount;
+
+ console.log(
+ `Deleted ${batchDeletedCount} links in batch ${batchIndex + 1}/${batches.length}`,
+ );
}
+ return {
+ deletedCount,
+ };
+}
+
+async function deleteLinksBatch(
+ links: ExpandedLink[],
+ options?: {
+ where?: Omit<Prisma.LinkWhereInput, "id">;
+ },
+): Promise<number> {
const linkIds = links.map((link) => link.id);
const discountCodes = await prisma.discountCode.findMany({
@@ -87,9 +118,5 @@ export async function deleteLinks(
]);
}
- console.log(`Deleted ${deletedCount} links.`);
-
- return {
- deletedCount,
- };
+ return deletedCount;
}