title: How to delete multiple objects with the same key in aws s3 bucket
body: <h2>TLDR; How can I delete image replicas in subfolders of s3 with the same key as the original image?</h2>
  
  <p>I've got a prisma server and upload images from my app to my s3 bucket through the prisma backend. Moreover, I run a lambda function to resize these images on-the-fly if requested.</p>
  
  <p><strong>Here's the process of the lambda function</strong> </p>
  
  <ul>
  <li>A user requests a resized asset from an S3 bucket through its static
  website hosting endpoint. The bucket has a routing rule configured to
  redirect to the resize API any request for an object that cannot be
  found. </li>
  <li>Because the resized asset does not exist in the bucket, the
  request is temporarily redirected to the resize API method. </li>
  <li>The user’s browser follows the redirect and requests the resize operation via API Gateway.</li>
  <li>The API Gateway method is configured to trigger a
  Lambda function to serve the request. </li>
  <li>The Lambda function downloads
  the original image from the S3 bucket, resizes it, and uploads the
  resized image back into the bucket as the originally requested key.</li>
  <li>When the Lambda function completes, API Gateway permanently redirects
  the user to the file stored in S3.</li>
  <li>The user’s browser requests the
  now-available resized image from the S3 bucket. </li>
  <li>Subsequent requests
  from this and other users will be served directly from S3 and bypass
  the resize operation. </li>
  <li>If the resized image is deleted in the future,
  the above process repeats and the resized image is re-created and
  replaced into the S3 bucket.</li>
  </ul>
  
  <p><a href="https://aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/" rel="nofollow noreferrer">https://aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/</a></p>
  
  <hr>
  
  <p>This brings me to the following issue:
  Whenever I delete an image-node with a key in Prisma I can delete the object with the same key from aws s3, yet I won't touch the resized replicas of it in the subfolders of the respective resolutions. How can I achieve this? I tried using aws' deleteObjects() by passing in only one key as shown below. However, this only deletes the original image at the root of the bucket.</p>
  
  <p><strong>Here's the lambda functions implementation</strong></p>
  
  <pre><code>exports.processDelete = async ( { id, key }, ctx, info) =&gt; {
  
    const params = {
      Bucket: 'XY',
      Delete: {
        Objects: [
          {
            Key: key, 
          },
        ],
        Quiet: false
      }
    }
  
    // Delete from S3
    const response = await s3
      .deleteObjects(
        params,
        function(err, data) {
          if (err) console.log(err, err.stack); // an error occurred
          else     console.log(data);           // successful response
        }
      ).promise()
  
    // Delete from Prisma
    await ctx.db.mutation.deleteFile({ where: { id } }, info)
  
    console.log('Successfully deleted file!')
  }
  </code></pre>