Skip to content

fix(storage): delete S3 objects recursively and report bulk delete failures - #13961

Closed
Neekoras wants to merge 2 commits into
appwrite:mainfrom
Neekoras:fix-13808-s3-recursive-delete
Closed

Neekoras wants to merge 2 commits into
appwrite:mainfrom
Neekoras:fix-13808-s3-recursive-delete

Conversation

@Neekoras

Copy link
Copy Markdown

What does this PR do?

Re-opened here at @ChiragAgg5k's request on utopia-php/monorepo#352: "Storage now lives in packages/storage in appwrite/appwrite (#13926), and this copy is being retired. @Neekoras, please re-open this against packages/storage there if you still want it." Same change, on this copy, plus the one review finding that PR received.

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.

The deletes worker removes a project's files that 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. This is the library half of #13808. The main symptom reported there, bucket deletion leaving files, comes from getDevice() folding the S3 bucket name into the device root, which makes listObjects() address the service endpoint instead of the bucket. That one is not fixed here: the root is part of the path stored on every existing file document, so changing it needs a migration, which is a maintainer's call.

Two more problems in the same code:

  • deletePath() lists with "{root}/{path}" as the prefix, with no trailing separator, so deletePath('app-1') also matches app-12/.... One directory's deletion can take a sibling whose name merely starts with the same characters, and project ids are generated, so that shape is reachable.
  • A bulk delete reports per-key failures inside the body of a 200. The result was never inspected, so a delete that S3 refused, for example with AccessDenied, was read as success.

Changes

  • delete($path, true) deletes every object under the path, then the key itself, matching Local, where the path may also be a file. Without $recursive the behaviour is unchanged: one DELETE.
  • The listing loop deletePath() already had moves into a private deleteByPrefix(), shared by both.
  • The prefix ends with /, so a listing cannot reach a sibling.
  • A bulk delete response carrying <Error> entries raises RemoteException naming the key, code and message. The request sets <Quiet>true</Quiet>, so a healthy response carries no entries at all.
  • delete('', true) does not recurse, so an empty path cannot become "list and delete the whole bucket".

No public API or signature changes.

Test Plan

Four cases in packages/storage/tests/Device/S3Test.php, through the existing scripted client, so the assertions are on the requests the device actually 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/
  • a bulk delete answering 200 with an AccessDenied entry raises instead of returning success

Three of the four fail on main: the first sends 1 request instead of 3, the third lists with prefix=root/app-1, and the fourth throws nothing.

cd packages/storage && composer install && composer test   # 123 tests, 395 assertions pass
php bin/monorepo check storage                             # Pint, PHPStan and Rector pass

The e2e tier needs Docker, which I do not have on this machine, so it did not run here.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? — no API metadata changed

…ilures

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, and a per-key failure reported inside a 200 bulk delete response is raised instead of being read as success.
@ghost

ghost commented Sep 28, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The previously reported XML-key issue is fixed, and I found no new issues requiring changes.

This pull request makes recursive S3 deletion remove objects beneath a path, limits directory deletion to its own prefix, and reports per-key bulk-delete failures. The latest commits escape object keys in bulk-delete XML, handle XML failures returned as undecoded strings, and replace request-focused deletion tests with in-memory bucket checks.

Latest changes: The newest commits escape listed keys in bulk-delete requests, decode nonempty raw XML responses before checking for errors, and add outcome-based deletion tests plus a raw-response failure test.

Verdict New comments Fixed Still open
💬 Commented 0 1 0

Note

@Neekoras does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve.

📂 Walkthrough · 2
File Change
packages/storage/src/Device/S3.php Adds recursive and prefix-bounded bulk deletion, escapes XML keys, and checks decoded or raw bulk-delete responses for errors.
packages/storage/tests/Device/S3Test.php Tests deletion outcomes with an in-memory bucket and verifies per-key failures from both decoded and raw XML responses.
✅ Fixed since the last review · 1
  • Escape listed keys before recursive bulk deletion · packages/storage/src/Device/S3.php:407

Reviewed the commits since dcf75e0 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Tier A · See the inline comments. Summary

Comment thread packages/storage/src/Device/S3.php Resolved
@greptile-apps

greptile-apps Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Critical risk] Changes how files are deleted from cloud storage.

The PR appears safe to merge based on the reviewed changes.

Summary

The PR makes recursive S3 deletion remove objects beneath a path, bounds directory listings to avoid prefix-sharing siblings, escapes keys in bulk-delete XML, and reports per-key deletion failures.

  • The revised tests check surviving objects and cover an undecoded XML failure response.

Reviews (2) · Last reviewed commit: "fix(storage): escape keys in the bulk de..."

Comment thread packages/storage/src/Device/S3.php Resolved
… response

A key holding & or < made the request malformed, and a failure that arrived undecoded read as success. The delete tests now assert which objects survive against an in-memory bucket rather than the shape of each request.
@Neekoras

Copy link
Copy Markdown
Author

Closing this: main has since covered both halves of it. 13bf781 deletes every object under the directory, f57d3f9 fails the delete when the service keeps keys, and 886fe9a surfaces the kept objects, so deletePath now paginates on IsTruncated/NextContinuationToken and reports partial failures. That is more thorough than what I had here, E2E coverage included.

Worth noting #13808 is still open even though the code looks fixed, in case someone wants to close it or there is a remaining case I am not seeing. If any part of what I had is still uncovered, I am happy to reopen with just that piece.

@Neekoras Neekoras closed this Oct 26, 2026
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.

1 participant