MCPcopy Index your code
hub / github.com/coder/mux / deleteWorkspace

Method deleteWorkspace

src/node/runtime/DockerRuntime.ts:910–1010  ·  view source on GitHub ↗
(
    projectPath: string,
    workspaceName: string,
    force: boolean,
    abortSignal?: AbortSignal
  )

Source from the content-addressed store, hash-verified

908 }
909
910 async deleteWorkspace(
911 projectPath: string,
912 workspaceName: string,
913 force: boolean,
914 abortSignal?: AbortSignal
915 ): Promise<{ success: true; deletedPath: string } | { success: false; error: string }> {
916 if (abortSignal?.aborted) {
917 return { success: false, error: "Delete operation aborted" };
918 }
919
920 const containerName = getContainerName(projectPath, workspaceName);
921 const deletedPath = CONTAINER_SRC_DIR;
922
923 try {
924 // Check if container exists
925 const inspectResult = await runDockerCommand(`docker inspect ${containerName}`, 10000);
926
927 if (inspectResult.exitCode !== 0) {
928 // Only treat as "doesn't exist" if Docker says so
929 if (inspectResult.stderr.toLowerCase().includes("no such object")) {
930 return { success: true, deletedPath };
931 }
932 return {
933 success: false,
934 error: `Docker error: ${inspectResult.stderr || inspectResult.stdout || "unknown error"}`,
935 };
936 }
937
938 if (!force) {
939 // Check if container is already running before we start it
940 const wasRunning = await runDockerCommand(
941 `docker inspect -f '{{.State.Running}}' ${containerName}`,
942 10000
943 );
944 const containerWasRunning =
945 wasRunning.exitCode === 0 && wasRunning.stdout.trim() === "true";
946
947 // Start container if stopped (docker start is idempotent - succeeds if already running)
948 const startResult = await runDockerCommand(`docker start ${containerName}`, 30000);
949 if (startResult.exitCode !== 0) {
950 // Container won't start - skip dirty checks, allow deletion
951 // (container is broken/orphaned, user likely wants to clean up)
952 } else {
953 // Helper to stop container if we started it (don't leave it running on check failure)
954 const stopIfWeStartedIt = async () => {
955 if (!containerWasRunning) {
956 await runDockerCommand(`docker stop ${containerName}`, 10000);
957 }
958 };
959
960 // Check for uncommitted changes
961 const checkResult = await runDockerCommand(
962 `docker exec ${containerName} bash -c 'cd ${CONTAINER_SRC_DIR} && git diff --quiet --exit-code && git diff --quiet --cached --exit-code'`,
963 10000
964 );
965
966 if (checkResult.exitCode !== 0) {
967 await stopIfWeStartedIt();

Callers

nothing calls this directly

Calls 3

getErrorMessageFunction · 0.90
getContainerNameFunction · 0.85
runDockerCommandFunction · 0.85

Tested by

no test coverage detected