The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
use the bulk delete method to delete the links
Kiran K53dba1ffb4e0
diff --git a/apps/web/app/api/cron/cleanup/route.ts b/apps/web/app/api/cron/cleanup/route.tsindex 7d025ef88f..6d9d8b2af0 100644--- a/apps/web/app/api/cron/cleanup/route.ts+++ b/apps/web/app/api/cron/cleanup/route.ts@@ -1,6 +1,6 @@import { deleteDomainAndLinks } from "@/lib/api/domains";import { handleAndReturnErrorResponse } from "@/lib/api/errors";-import { deleteLink } from "@/lib/api/links";+import { bulkDeleteLinks } from "@/lib/api/links/bulk-delete-links";import { verifyVercelSignature } from "@/lib/cron/verify-vercel";import { prisma } from "@/lib/prisma";import { log } from "@dub/utils";@@ -12,7 +12,7 @@ const E2E_USER_ID = "clxz1q7c7000hbqx5ckv4r82h";const E2E_WORKSPACE_ID = "clrei1gld0002vs9mzn93p8ik";// Cron to remove links and domains created during the E2E test.-// Runs once every day at noon UTC (0 12 * * *)+// Run every 6 hours (0 */6 * * *)export async function GET(req: Request) {try {await verifyVercelSignature(req);@@ -28,8 +28,8 @@ export async function GET(req: Request) {lt: oneHourAgo,},},- select: {- id: true,+ include: {+ tags: true,},take: 100,}),@@ -50,11 +50,24 @@ export async function GET(req: Request) {}),]);+ // Delete the linksif (links.length > 0) {- // TODO: use bulk delete instead- await Promise.all(links.map((link) => deleteLink(link.id)));+ await prisma.link.deleteMany({+ where: {+ id: {+ in: links.map((link) => link.id),+ },+ },+ });++ // Post delete cleanup+ await bulkDeleteLinks({+ workspaceId: E2E_WORKSPACE_ID,+ links,+ });}+ // Delete the domainsif (domains.length > 0) {await Promise.all(domains.map((domain) => deleteDomainAndLinks(domain.slug)),diff --git a/apps/web/lib/api/links/bulk-delete-links.ts b/apps/web/lib/api/links/bulk-delete-links.tsnew file mode 100644index 0000000000..61f20bb37e--- /dev/null+++ b/apps/web/lib/api/links/bulk-delete-links.ts@@ -0,0 +1,61 @@+import { prisma } from "@/lib/prisma";+import { storage } from "@/lib/storage";+import { recordLink } from "@/lib/tinybird";+import { redis } from "@/lib/upstash";+import { R2_URL } from "@dub/utils";+import { Link, Tag } from "@prisma/client";++export async function bulkDeleteLinks({+ workspaceId,+ links,+}: {+ workspaceId: string;+ links: (Link & { tags: Pick<Tag, "id">[] })[];+}) {+ if (links.length === 0) {+ return;+ }++ const pipeline = redis.pipeline();++ links.forEach((link) => {+ pipeline.hdel(link.domain.toLowerCase(), link.key.toLowerCase());+ });++ return await Promise.all([+ // Delete the images+ // TODO: How do we optimize this?+ links+ .filter((link) => link.image?.startsWith(`${R2_URL}/images/${link.id}`))+ .map((link) => storage.delete(link.image!.replace(`${R2_URL}/`, ""))),++ // Delete the links from Redis+ pipeline.exec(),++ // Record the links deletion in Tinybird+ recordLink(+ links.map((link) => ({+ link_id: link.id,+ domain: link.domain,+ key: link.key,+ url: link.url,+ workspace_id: link.projectId,+ created_at: link.createdAt,+ tag_ids: link.tags.map(({ id }) => id),+ deleted: true,+ })),+ ),++ // Decrement the workspace's links usage+ prisma.project.update({+ where: {+ id: workspaceId,+ },+ data: {+ linksUsage: {+ decrement: links.length,+ },+ },+ }),+ ]);+}