From 76038f6db60a0b7688b19cdb882c6b36923f1dd7 Mon Sep 17 00:00:00 2001 From: Mauricio Siu Date: Mon, 16 Feb 2026 22:19:57 -0600 Subject: [PATCH] refactor(deployments): streamline deployment clearing process and remove cloud check - Removed the cloud check from the ClearDeployments component, simplifying the logic. - Updated the clearOldDeployments function to accept appName and serverId, enhancing its flexibility. - Adjusted the return values in the application and compose routers to return a boolean instead of a detailed message, improving consistency. --- .../deployments/clear-deployments.tsx | 14 ++------- .../dokploy/server/api/routers/application.ts | 8 ++--- apps/dokploy/server/api/routers/compose.ts | 8 ++--- packages/server/src/services/deployment.ts | 30 ++++++++----------- 4 files changed, 19 insertions(+), 41 deletions(-) diff --git a/apps/dokploy/components/dashboard/application/deployments/clear-deployments.tsx b/apps/dokploy/components/dashboard/application/deployments/clear-deployments.tsx index 70f9d2554..75862dfff 100644 --- a/apps/dokploy/components/dashboard/application/deployments/clear-deployments.tsx +++ b/apps/dokploy/components/dashboard/application/deployments/clear-deployments.tsx @@ -25,11 +25,6 @@ export const ClearDeployments = ({ id, type }: Props) => { type === "application" ? api.application.clearDeployments.useMutation() : api.compose.clearDeployments.useMutation(); - const { data: isCloud } = api.settings.isCloud.useQuery(); - - if (isCloud) { - return null; - } return ( @@ -57,14 +52,11 @@ export const ClearDeployments = ({ id, type }: Props) => { applicationId: id || "", composeId: id || "", }) - .then(async (result) => { - toast.success( - `${result.deletedCount} old deployments cleared successfully`, - ); - // Invalidate deployment queries to refresh the list + .then(async () => { + toast.success("Old deployments cleared successfully"); await utils.deployment.allByType.invalidate({ id, - type, + type: type as "application" | "compose", }); }) .catch((err) => { diff --git a/apps/dokploy/server/api/routers/application.ts b/apps/dokploy/server/api/routers/application.ts index 157a89630..391191ce3 100644 --- a/apps/dokploy/server/api/routers/application.ts +++ b/apps/dokploy/server/api/routers/application.ts @@ -761,12 +761,8 @@ export const applicationRouter = createTRPCRouter({ "You are not authorized to clear deployments for this application", }); } - const result = await clearOldDeployments(input.applicationId); - return { - success: true, - message: `${result.deletedCount} old deployments cleared successfully`, - deletedCount: result.deletedCount, - }; + await clearOldDeployments(application.appName, application.serverId); + return true; }), killBuild: protectedProcedure .input(apiFindOneApplication) diff --git a/apps/dokploy/server/api/routers/compose.ts b/apps/dokploy/server/api/routers/compose.ts index de99e4d7f..665b6770b 100644 --- a/apps/dokploy/server/api/routers/compose.ts +++ b/apps/dokploy/server/api/routers/compose.ts @@ -278,12 +278,8 @@ export const composeRouter = createTRPCRouter({ "You are not authorized to clear deployments for this compose", }); } - const result = await clearOldDeployments(input.composeId, "compose"); - return { - success: true, - message: `${result.deletedCount} old deployments cleared successfully`, - deletedCount: result.deletedCount, - }; + await clearOldDeployments(compose.appName, compose.serverId); + return true; }), killBuild: protectedProcedure .input(apiFindCompose) diff --git a/packages/server/src/services/deployment.ts b/packages/server/src/services/deployment.ts index 5af7ebc80..24e1590a9 100644 --- a/packages/server/src/services/deployment.ts +++ b/packages/server/src/services/deployment.ts @@ -19,7 +19,7 @@ import { } from "@dokploy/server/utils/process/execAsync"; import { TRPCError } from "@trpc/server"; import { format } from "date-fns"; -import { and, desc, eq, or } from "drizzle-orm"; +import { desc, eq } from "drizzle-orm"; import { type Application, findApplicationById, @@ -853,23 +853,17 @@ export const findAllDeploymentsByServerId = async (serverId: string) => { }; export const clearOldDeployments = async ( - id: string, - type: "application" | "compose" = "application", + appName: string, + serverId: string | null, ) => { - // Get all deployments ordered by creation date (newest first) - const deploymentsList = await db.query.deployments.findMany({ - where: and( - eq(deployments[`${type}Id`], id), - or(eq(deployments.status, "done"), eq(deployments.status, "error")), - ), - orderBy: desc(deployments.createdAt), - }); - - for (const deployment of deploymentsList) { - await removeDeployment(deployment.deploymentId); + const { LOGS_PATH } = paths(!!serverId); + const folder = path.join(LOGS_PATH, appName); + const command = ` + rm -rf ${folder}; + `; + if (serverId) { + await execAsyncRemote(serverId, command); + } else { + await execAsync(command); } - - return { - deletedCount: deploymentsList.length, - }; };