diff --git a/internal/handlers/app.go b/internal/handlers/app.go index 2887139..f5893f2 100644 --- a/internal/handlers/app.go +++ b/internal/handlers/app.go @@ -267,6 +267,29 @@ func (h *Handlers) HandleAppDelete() http.HandlerFunc { return } + // Stop and remove the Docker container before deleting the DB record + containerInfo, containerErr := h.docker.FindContainerByAppID(request.Context(), appID) + if containerErr == nil && containerInfo != nil { + if containerInfo.Running { + stopErr := h.docker.StopContainer(request.Context(), containerInfo.ID) + if stopErr != nil { + h.log.Error("failed to stop container during app deletion", + "error", stopErr, "app", application.Name, + "container", containerInfo.ID) + } + } + + removeErr := h.docker.RemoveContainer(request.Context(), containerInfo.ID, true) + if removeErr != nil { + h.log.Error("failed to remove container during app deletion", + "error", removeErr, "app", application.Name, + "container", containerInfo.ID) + } else { + h.log.Info("removed container during app deletion", + "app", application.Name, "container", containerInfo.ID) + } + } + deleteErr := application.Delete(request.Context()) if deleteErr != nil { h.log.Error("failed to delete app", "error", deleteErr)