Home » Php » php – S3 DeleteObject – DeleteMarker always returns empty

php – S3 DeleteObject – DeleteMarker always returns empty

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am using the AWS SDK for PHP, version 2.4.7 installed via composer. After deleting a file from an S3 bucket the DeleteMarker key in the response object is always empty even through the file has actually been deleted from S3. The documentation states that DeleteMarker should be true if the operation was successful otherwise it’s false.

My delete call is:

    // delete S3 object
    $result = $s3->deleteObject(array(
        'Bucket' => $this->_bucket,
        'Key' => $object_key,
    ));

and the response is:

Guzzle\Service\Resource\Model Object
(
    [structure:protected] =>
    [data:protected] => Array
        (
            [DeleteMarker] =>
            [VersionId] =>
            [RequestId] => 2CC3EC60C4294CB5
        )
)

If I then do:

    // check if was deleted
    $is_deleted = (bool) $result->get('DeleteMarker');

$is_deleted is always false. How can it be that there is no value returned against the DeleteMarker key even though the delete operation was actually successful and the file was removed from S3?

UPDATE:

If I add a slash to the start of my key I get a response false back even though the file is still removed from S3.

Key “path/to/my/image.jpg” results in DeleteMarker having empty value
Key “/path/to/my/image.jpg” results in DeleteMarker having empty false

But in both cases the images is removed from the S3 bucket.

How to&Answers:

In converting from SDK v. 1.? to 2.?, I too ran into the problem of not knowing if the file deleted (there used to be the ->isOK() method on just about everything that would let me know if the file had been deleted or not).

I finally stumbled upon this response from the Guzzle creator: https://forums.aws.amazon.com/thread.jspa?messageID=455154

Basically, there is no longer any ‘did delete’ flag of any kind. What Michael (Guzzle) suggests is this: if you want to know if a file deleted, use ->deleteObject() and then run ->doesObjectExist() to see if the delete was successful.

The rationale for the change is this: the new approach lets you fire off tons of delete requests without having to wait for replies, etc.

For what it’s worth.
David

Answer:

I am having the same issue with the Javascript SDK. The call to deleteObject returns fine (HTTP 204) regardless if the file exists or not! This makes it impossible to tell if the file was deleted from the response code. Furthermore, it seems that the response only includes DeleteMarker if the bucket has versioning enabled (see also this thread on DeleteMarker).

I see two possibilities to work around this issue.

As first option, you can enable versioning and use DELETE Object versionID to permanently delete your objects (see the AWS documentation). This will require you to either store the versionID in your database, or query it prior to deletion using listObjectVersions

As second option, you can use listObjects to check if the file exists, delete the file using deleteObject and check listObjects again to make sure the file was deleted for sure.

I am not satisfied with either solution, but they do the job for now