Skip to main content
Permanently delete a comment from a task. This automatically decrements the task’s amount_of_comments counter.

Endpoint

DELETE /api/deleteCommentFromTask

Request Body

comment_id
number
required
The ID of the comment to delete.
task_id
number
required
The ID of the task that the comment belongs to. Required to update the comment counter.

Response

success
object
Returns the deleted comment object when successful.
error
string
Returns "Invalid!" if the deletion fails (e.g., comment not found).

Example Request

curl -X DELETE http://localhost:5173/api/deleteCommentFromTask \
  -H "Content-Type: application/json" \
  -d '{
    "comment_id": 15,
    "task_id": 42
  }'

Response Example

Success

Returns the deleted comment object:
{
  "id": 15,
  "task_id": 42,
  "comment": "Client requested to move the shoot to 2pm",
  "created_at": "2026-03-01T14:30:00.000Z"
}

Error

"Invalid!"

Behavior Notes

Deletion is permanent and cannot be undone. The comment is immediately removed from the database.
Deleting a comment decrements the task’s amount_of_comments field by 1.
After successfully deleting a comment, emit a database-change event via Socket.io to notify other clients. See Real-time Updates for details.

Database Operations

The endpoint performs two Prisma operations:
  1. Delete the comment:
const tasks = await prisma.task_comments.delete({
  where: { id: comment_id }
});
  1. Decrement the comment counter:
await prisma.tasks.update({
  where: { id: task_id },
  data: {
    amount_of_comments: { decrement: 1 }
  }
});

Common Use Cases

Remove Outdated Comment

async function removeOutdatedComment(commentId, taskId) {
  const confirmed = confirm('Remove this comment?');
  
  if (confirmed) {
    try {
      const deleted = await deleteComment(commentId, taskId);
      console.log('Comment removed:', deleted.comment);
    } catch (error) {
      alert('Failed to remove comment');
    }
  }
}

Delete All Comments for a Task

async function deleteAllTaskComments(taskId) {
  // First, fetch all comments for the task
  const comments = await prisma.task_comments.findMany({
    where: { task_id: taskId }
  });
  
  // Delete each comment
  const results = await Promise.allSettled(
    comments.map(c => deleteComment(c.id, taskId))
  );
  
  const successCount = results.filter(r => r.status === 'fulfilled').length;
  console.log(`Deleted ${successCount} comments from task ${taskId}`);
}

Safe Delete with Validation

async function safeDeleteComment(commentId, taskId) {
  const response = await fetch('/api/deleteCommentFromTask', {
    method: 'DELETE',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ comment_id: commentId, task_id: taskId })
  });
  
  const result = await response.json();
  
  if (result === 'Invalid!') {
    throw new Error(`Failed to delete comment ${commentId}`);
  }
  
  return result;
}

Error Handling

Common scenarios that trigger the error response:
  • Comment ID does not exist
  • Task ID does not exist
  • Database connection error
  • Invalid ID format (non-numeric)

Cascade Deletion

If you delete a task using Delete Task, all associated comments are automatically deleted due to the onDelete: Cascade constraint in the schema. You don’t need to manually delete comments first.
From the Prisma schema:
model task_comments {
  id         Int      @id @default(autoincrement())
  task       Tasks    @relation(fields: [task_id], references: [id], onDelete: Cascade)
  task_id    Int
  comment    String
  created_at DateTime @default(now())
}

Why Both IDs Are Required

The endpoint requires both comment_id and task_id because:
  1. comment_id is used to identify and delete the comment
  2. task_id is used to decrement the task’s comment counter
If you only have the comment_id, you can query the comment first to get the task_id:
const comment = await prisma.task_comments.findUnique({
  where: { id: commentId }
});

if (comment) {
  await deleteComment(commentId, comment.task_id);
}

Source Reference

Implementation: src/routes/api/(comments)/deleteCommentFromTask/+server.ts:5

Build docs developers (and LLMs) love