Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
Deleting from s3 bucket and database. Run one function after another

I'm trying to run an API call that deletes an image from an S3 bucket and MySQL db but running into an issue where the s3 image can't be found as it's being deleted from the database first.

// Router code:

//req.body is an array of ids. e.g. [ 213, 214]
//remove from s3

req.body.forEach((mediaId) => {
    mediaService.removeMediaItem(mediaId);
});


//remove from db

await recycleBinService.deleteRecycledMedia(req.body);

Ideally this code in the router runs delete from s3 > delete from db but currently I think it's deleting from db first as the image variable in the removeMediaItem function is showing as null if I log it out.

// removeMediaItem

const removeMediaItem = async (mediaId) => {
  const image = await _db.findOne("media", { id: mediaId });
  console.log("image: ", image);
  await s3Connection.deleteFile(image.file_name);
};
// deleteRecycledMedia

const wheresMedia = _apiDb.orWhere("id", mediaIds);
const wheresRecycle = _apiDb.orWhere("media", mediaIds);

// db Query that second input after first.

await _apiDb.doubleRawSql(
`DELETE from media ${wheresMedia}`,
`DELETE from recycle_bin ${wheresRecycle}`
  );
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to await removeMediaItem. i.e.

req.body.forEach((mediaId) => {
  await mediaService.removeMediaItem(mediaId);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!