Skip to content

fix(storage): delete S3 objects recursively and bound the prefix at a separator - #352

Closed
Neekoras wants to merge 1 commit into
utopia-php:mainfrom
Neekoras:storage-s3-recursive-delete
Closed

Neekoras wants to merge 1 commit into
utopia-php:mainfrom
Neekoras:storage-s3-recursive-delete

Conversation

@Neekoras

Copy link
Copy Markdown

Problem

Device::delete(string $path, bool $recursive = false) is implemented by Local, which walks the tree when $recursive is true, and by S3, which declares the parameter and never reads it. S3::delete($path, true) sends one DELETE for the directory key. S3 has no directories, so that request removes nothing and returns success, and every object under the path stays in the bucket.

Appwrite deletes a project's files this way, delete($device->getRoot(), true) on the files, sites, functions, builds and cache devices, so on any S3 device a deleted project's objects are left behind, AWS included. Reported in appwrite/appwrite#13808, whose main symptom is a separate Appwrite-side configuration problem; this is the library half.

A second, quieter problem in the same area: deletePath() lists with "{root}/{path}" as the prefix, with no trailing separator. deletePath('app-1') matches app-12/... too, so deleting one directory can delete a sibling whose name merely starts with the same characters. With Appwrite's app-<projectId> layout, project ids are generated, so a collision of that shape is possible.

Change

  • delete($path, true) deletes every object under the path, then the key itself, which matches Local, where the path may also be a file. Without $recursive the behaviour is byte for byte what it was: one DELETE.
  • The listing loop that deletePath() already had moves into a private deleteByPrefix(), shared by both.
  • The prefix ends with /, so a listing is bounded by a separator and cannot reach a sibling.
  • delete('', true) does not recurse, so an empty path cannot turn into "list and delete the whole bucket".

No public API or signature changes.

Tests

Three cases in packages/storage/tests/Storage/Device/S3Test.php, through the existing scripted client, so the assertions are on the HTTP requests the device makes:

  • a recursive delete lists with prefix=root/dir/, sends the batch delete containing both listed keys, then deletes the key itself
  • a non-recursive delete still sends exactly one DELETE
  • deletePath('app-1') lists with prefix=root/app-1/

The first and third fail without the change: the first sends 1 request instead of 3, the third lists with prefix=root/app-1.

Verification

  • bin/monorepo test storage: 122 tests, 393 assertions pass. The e2e tier needs Docker, which I do not have locally, so it did not run.
  • bin/monorepo check storage: Pint, PHPStan and Rector all pass.

… separator

S3::delete() accepted a $recursive flag and ignored it, so a caller deleting a tree left every object behind. The listing prefix now ends with a separator so a sibling with a shared name prefix is not deleted.
@walter-o-brien

walter-o-brien Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

🔍 OpenCodeReview found 1 issue(s) in this PR.

  • ✅ Successfully posted inline: 1 comment(s)

Comment on lines +407 to +409
if ($recursive && $path !== '') {
$this->deleteByPrefix($path);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug · medium
S3 DeleteObjects can return HTTP 200 while reporting per-key failures in its XML body. deleteByPrefix() currently ignores that response and returns true, so this new recursive path can leave objects behind yet continue to the exact-key DELETE and report success. Inspect the bulk-delete result and surface any <Error> entries before treating the recursive delete as successful.

@ChiragAgg5k

Copy link
Copy Markdown
Member

Storage now lives in packages/storage in appwrite/appwrite (appwrite/appwrite#13926), and this copy is being retired. @Neekoras, please re-open this against packages/storage there if you still want it.

@Neekoras

Copy link
Copy Markdown
Author

Thanks, re-opened as appwrite/appwrite#13961 against packages/storage. It also carries the bulk-delete fix OpenCodeReview asked for here: a per-key failure inside a 200 response now raises instead of reading as success.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants