Next.js Discord

Discord Forum

s3 bucket DELETE shows 204 response but not deleting?

Unanswered
Waterman posted this in #help-forum
Open in Discord
Avatar
WatermanOP
Hello,
I am trying to delete files/images by matching the correct key that is about to be deleted with the one in the s3 bucket.

I do get a 204 response from S3 and it says everything was successfull but when I check if the file is gone, it is not.

The key that I am passing from the frontend to the server is correct and matches the one I want to delete in the S3 Bucket.
But still it does not dissapear from the bucket, no errors, just successfull responses.

code:
export const DELETE = async (req: NextRequest) => {
  const session = await getServerSession(options);

  if (!session) {
    throw "Not Admin";
  }

  // Get the key of the object to be deleted from the request.
  const { key } = await req.json();

  if (!key) {
    return NextResponse.json({ error: "Object key is required" });
  }

  const client = new S3Client({
    region: "eu-north-1",
    credentials: {
      accessKeyId: process.env.S3_ACCESS_KEY || "",
      secretAccessKey: process.env.S3_SECRET_ACCESS_KEY || "",
    },
  });

  try {
    const result = await client.send(
      new DeleteObjectCommand({
        Bucket: "leveccobucket",
        Key: key,
      })
    );
    console.log(result);

    return NextResponse.json({
      success: true,
      message: "Object deleted successfully",
    });
  } catch (error) {
    return NextResponse.json({ success: false });
  }
};

0 Replies