Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions plugins/sentry-cli/skills/sentry-cli/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ Create a new project
- `--json - Output as JSON`
- `--fields <value> - Comma-separated fields to include in JSON output (dot.notation supported)`

#### `sentry project delete <org/project>`

Delete a project

**Flags:**
- `-y, --yes - Skip confirmation prompt`
- `-n, --dry-run - Validate inputs and show what would be deleted without deleting it`
- `--json - Output as JSON`
- `--fields <value> - Comma-separated fields to include in JSON output (dot.notation supported)`

#### `sentry project list <org/project>`

List projects
Expand Down
199 changes: 199 additions & 0 deletions src/commands/project/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/**
* sentry project delete
*
* Permanently delete a Sentry project.
*
* ## Flow
*
* 1. Parse target arg → extract org/project (e.g., "acme/my-app" or "my-app")
* 2. Verify the project exists via `getProject` (also displays its name)
* 3. Prompt for confirmation (unless --yes is passed)
* 4. Call `deleteProject` API
* 5. Display result
*
* Safety measures:
* - No auto-detect mode: requires explicit target to prevent accidental deletion
* - Confirmation prompt with strict `confirmed !== true` check (Symbol(clack:cancel) gotcha)
* - Refuses to run in non-interactive mode without --yes flag
*/

import { isatty } from "node:tty";
import type { SentryContext } from "../../context.js";
import { deleteProject, getProject } from "../../lib/api-client.js";
import { parseOrgProjectArg } from "../../lib/arg-parsing.js";
import { buildCommand } from "../../lib/command.js";
import { ApiError, CliError, ContextError } from "../../lib/errors.js";
import { logger } from "../../lib/logger.js";
import { resolveOrgProjectTarget } from "../../lib/resolve-target.js";
import { buildProjectUrl } from "../../lib/sentry-urls.js";

const log = logger.withTag("project.delete");

/** Command name used in error messages and resolution hints */
const COMMAND_NAME = "project delete";

/**
* Prompt for confirmation before deleting a project.
*
* Throws in non-interactive mode without --yes. Returns true if confirmed,
* false if the user cancels.
*
* @param orgSlug - Organization slug for display
* @param project - Project with slug and name for display
* @returns true if confirmed, false if cancelled
*/
async function confirmDeletion(
orgSlug: string,
project: { slug: string; name: string }
): Promise<boolean> {
if (!isatty(0)) {
throw new CliError(
`Refusing to delete '${orgSlug}/${project.slug}' in non-interactive mode. Use --yes to confirm.`
);
}

const confirmed = await log.prompt(
`Delete project '${project.name}' (${orgSlug}/${project.slug})? This cannot be undone.`,
{ type: "confirm", initial: false }
);

// consola prompt returns Symbol(clack:cancel) on Ctrl+C — a truthy value.
// Strictly check for `true` to avoid deleting on cancel.
return confirmed === true;
}

/**
* Write dry-run output describing what would be deleted.
*
* @param stdout - Output stream
* @param orgSlug - Organization slug
* @param project - Project details
* @param json - Whether to output JSON
*/
function writeDryRunOutput(
stdout: { write: (s: string) => unknown },
orgSlug: string,
project: { slug: string; name: string },
json: boolean
): void {
if (json) {
stdout.write(
`${JSON.stringify({ dryRun: true, org: orgSlug, project: project.slug, name: project.name, url: buildProjectUrl(orgSlug, project.slug) })}\n`
);
} else {
stdout.write(
`Would delete project '${project.name}' (${orgSlug}/${project.slug}).\n` +
` URL: ${buildProjectUrl(orgSlug, project.slug)}\n`
);
}
}

type DeleteFlags = {
readonly yes: boolean;
readonly "dry-run": boolean;
readonly json: boolean;
readonly fields?: string[];
};

export const deleteCommand = buildCommand({
docs: {
brief: "Delete a project",
fullDescription:
"Permanently delete a Sentry project. This action cannot be undone.\n\n" +
"Requires explicit target — auto-detection is disabled for safety.\n\n" +
"Examples:\n" +
" sentry project delete acme-corp/my-app\n" +
" sentry project delete my-app\n" +
" sentry project delete acme-corp/my-app --yes\n" +
" sentry project delete acme-corp/my-app --dry-run",
},
output: "json",
parameters: {
positional: {
kind: "tuple",
parameters: [
{
placeholder: "org/project",
brief: "<org>/<project> or <project> (search across orgs)",
parse: String,
},
],
},
flags: {
yes: {
kind: "boolean",
brief: "Skip confirmation prompt",
default: false,
},
"dry-run": {
kind: "boolean",
brief:
"Validate inputs and show what would be deleted without deleting it",
default: false,
},
},
aliases: { y: "yes", n: "dry-run" },
},
async func(this: SentryContext, flags: DeleteFlags, target: string) {
const { stdout, cwd } = this;

// Block auto-detect for safety — destructive commands require explicit targets
const parsed = parseOrgProjectArg(target);
if (parsed.type === "auto-detect") {
throw new ContextError(
"Project target",
`sentry ${COMMAND_NAME} <org>/<project>`,
[
"Auto-detection is disabled for delete — specify the target explicitly",
]
);
}

const { org: orgSlug, project: projectSlug } =
await resolveOrgProjectTarget(parsed, cwd, COMMAND_NAME);

// Verify project exists before prompting — also used to display the project name
const project = await getProject(orgSlug, projectSlug);

// Dry-run mode: show what would be deleted without deleting it
if (flags["dry-run"]) {
writeDryRunOutput(stdout, orgSlug, project, flags.json);
return;
}

// Confirmation gate
if (!flags.yes) {
const confirmed = await confirmDeletion(orgSlug, project);
if (!confirmed) {
stdout.write("Cancelled.\n");
return;
}
}

try {
await deleteProject(orgSlug, project.slug);
} catch (error) {
if (error instanceof ApiError && error.status === 403) {
throw new ApiError(
`Permission denied: You don't have permission to delete '${orgSlug}/${project.slug}'.\n\n` +
"Project deletion requires the 'project:admin' scope.\n" +
" Re-authenticate: sentry auth login",
403,
error.detail,
error.endpoint
);
}
throw error;
}

if (flags.json) {
stdout.write(
`${JSON.stringify({ deleted: true, org: orgSlug, project: project.slug })}\n`
);
} else {
stdout.write(
`Deleted project '${project.name}' (${orgSlug}/${project.slug}).\n`
);
}
},
});
2 changes: 2 additions & 0 deletions src/commands/project/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { buildRouteMap } from "@stricli/core";
import { createCommand } from "./create.js";
import { deleteCommand } from "./delete.js";
import { listCommand } from "./list.js";
import { viewCommand } from "./view.js";

export const projectRoute = buildRouteMap({
routes: {
create: createCommand,
delete: deleteCommand,
list: listCommand,
view: viewCommand,
},
Expand Down
25 changes: 25 additions & 0 deletions src/lib/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
addAnOrganizationMemberToATeam,
createANewProject,
createANewTeam,
deleteAProject,
listAnOrganization_sIssues,
listAnOrganization_sProjects,
listAnOrganization_sRepositories,
Expand Down Expand Up @@ -719,6 +720,30 @@ export async function createProject(
return data as unknown as SentryProject;
}

/**
* Delete a project from an organization.
*
* Sends a DELETE request to the Sentry API. Returns 204 No Content on success.
*
* @param orgSlug - The organization slug
* @param projectSlug - The project slug to delete
* @throws {ApiError} 403 if the user lacks permission, 404 if the project doesn't exist
*/
export async function deleteProject(
orgSlug: string,
projectSlug: string
): Promise<void> {
const config = await getOrgSdkConfig(orgSlug);
const result = await deleteAProject({
...config,
path: {
organization_id_or_slug: orgSlug,
project_id_or_slug: projectSlug,
},
});
unwrapResult(result, "Failed to delete project");
}

/**
* Create a new team in an organization and add the current user as a member.
*
Expand Down
1 change: 1 addition & 0 deletions src/lib/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function getClientId(): string {
const SCOPES = [
"project:read",
"project:write",
"project:admin",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would happen if someone doesn't have this permission? would it face trouble signing in?

"org:read",
"event:read",
"event:write",
Expand Down
Loading
Loading