Skip to main content
DELETE
/
api
/
post
/
{id}
Delete Post
curl --request DELETE \
  --url https://api.example.com/api/post/{id}
{
  "code": 123,
  "status": "<string>",
  "post": {
    "id": 123,
    "user_id": 123,
    "category_id": 123,
    "title": "<string>",
    "content": "<string>",
    "image": "<string>",
    "created_at": "<string>",
    "updated_at": "<string>"
  },
  "message": "<string>"
}

Overview

This endpoint deletes a blog post. Users can only delete posts they have created. The system verifies ownership by matching the post’s user_id with the authenticated user’s ID from the JWT token.
This endpoint requires authentication and permanently deletes the post. This action cannot be undone.

Endpoint

DELETE /api/post/{id}

Authentication

Required. This endpoint uses the api.auth middleware to verify the JWT token. Header:
Authorization: Bearer YOUR_JWT_TOKEN

Path Parameters

id
number
required
The unique identifier of the post to delete

Request

No additional parameters required in the request body.

Response

Success Response (200)

code
number
required
HTTP status code (200 for success)
status
string
required
Status message (“success”)
post
object
required
The deleted post object
id
number
Unique identifier for the deleted post
user_id
number
ID of the user who created the post
category_id
number
ID of the post’s category
title
string
Title of the deleted post
content
string
Content of the deleted post
image
string
Filename of the post’s image
created_at
string
Timestamp when the post was created
updated_at
string
Timestamp when the post was last updated
{
  "code": 200,
  "status": "success",
  "post": {
    "id": 15,
    "user_id": 5,
    "category_id": 2,
    "title": "My Post Title",
    "content": "This is the post content...",
    "image": "1678901234photo.jpg",
    "created_at": "2026-03-04T14:25:30.000000Z",
    "updated_at": "2026-03-04T14:25:30.000000Z"
  }
}

Error Response (404) - Post Not Found or Unauthorized

code
number
HTTP status code (404 for not found)
status
string
Status message (“error”)
message
string
Error description
{
  "code": 404,
  "status": "error",
  "message": "El post no existe"
}
The error message “El post no existe” (The post does not exist) is returned both when the post doesn’t exist and when you attempt to delete a post that doesn’t belong to you.

Authorization

The endpoint verifies ownership before allowing deletion:
$post = Post::where('id', $id)
    ->where('user_id', $user->sub)
    ->first();
You can only delete posts that you have created. Attempting to delete another user’s post will result in a 404 error.

Implementation Details

The user identity is extracted using the getIdentity() helper method:
private function getIdentity(Request $request) {
    $jwtAuth = new JwtAuth();
    $token = $request->header('Authorization', null);
    $user = $jwtAuth->checkToken($token, true);
    return $user;
}
The post is then located using both the post ID and the user ID from the token:
$post = Post::where('id', $id)
    ->where('user_id', $user->sub)
    ->first();

if(!empty($post)) {
    $post->delete();
}
This is a permanent delete operation. The post data is removed from the database and cannot be recovered. The associated image file is NOT automatically deleted from storage.

Build docs developers (and LLMs) love