title: How to delete an object in an S3 Bucket when file has been deleted locally in PHP
body: <p>I am trying to implement a feature which would help delete an object in my s3 bucket at the click of a button on my website. So far i have been able to successfully implement uploading of files, but deleting them using the AWS SDK for php seems impossible. Below is what i have implemented.</p>
  <pre><code>&lt;?php
  
  require 'vendor/autoload.php';
  
  use Aws\S3\S3Client;
  use Aws\S3\Exception\S3Exception;
  
  class Delete{
  function deleteS3object(string $bucketname, string $keyfilename){
  
     $bucket = $bucketname;
     $keyname = $keyfilename;
     
     $s3 = new S3Client([
         'version' =&gt; 'latest',
         'region'  =&gt; 'us-east-2'
     ]);
     
     // 1. Delete the object from the bucket.
     try
     {
         echo 'Attempting to delete ' . $keyname . '...' . PHP_EOL;
     
         $result = $s3-&gt;deleteObject([
             'Bucket' =&gt; $bucket,
             'Key'    =&gt; $keyname
         ]);
     
         if ($result['DeleteMarker'])
         {
             echo $keyname . ' was deleted or does not exist.' . PHP_EOL;
         } else {
             exit('Error: ' . $keyname . ' was not deleted.' . PHP_EOL);
         }
     }
     catch (S3Exception $e) {
         exit('Error: ' . $e-&gt;getAwsErrorMessage() . PHP_EOL);
     }
     
     // 2. Check to see if the object was deleted.
     try
     {
         echo 'Checking to see if ' . $keyname . ' still exists...' . PHP_EOL;
     
         $result = $s3-&gt;getObject([
             'Bucket' =&gt; $bucket,
             'Key'    =&gt; $keyname
         ]);
     
         echo 'Error: ' . $keyname . ' still exists.';
     }
     catch (S3Exception $e) {
         exit($e-&gt;getAwsErrorMessage());
     } 
     
     return;
  }
  }
  
  $x =  new Delete();
  $success = $x-&gt;deleteS3object('bucketname', 'file.png') ? 'deleted': 'Error';
  echo $success;
  ?&gt;
  </code></pre>
  <p>But it returns a 500 error notice for me, is there anything am possibly doing wrong?</p>