Skip to content

fix: delete incomplete attachments when media upload finalization fails - #85189

Open
elphizu wants to merge 2 commits into
WordPress:trunkfrom
elphizu:fix/upload-media-cleanup-on-finalize-failure
Open

elphizu wants to merge 2 commits into
WordPress:trunkfrom
elphizu:fix/upload-media-cleanup-on-finalize-failure

Conversation

@elphizu

@elphizu elphizu commented Oct 20, 2026

Copy link
Copy Markdown

What?

Closes #85158.
Delete incomplete attachments when an upload is cancelled after its original file has reached the server, including when finalization fails.

Why?

A failed /finalize request shows an upload error but leaves the attachment in the Media Library without finalized metadata. Retrying the upload creates a duplicate.

How?

Move attachment cleanup into cancelItem() for top-level items with an attachment ID. Remove the separate deletion from the child total-failure path so the parent attachment is deleted once.

Cleanup also applies to manual and silent cancellation. Child sideload cancellation preserves the parent when some sub-sizes succeeded. Deletion is best-effort and does not replace the original upload error.

Testing Instructions

  1. Open a new post in Chrome 137+ with client-side media processing active.
  2. In DevTools, enable request blocking for *://localhost:8888/wp-json/wp/v2/media/*/finalize*, adjusting the origin for your environment.
  3. Upload a JPEG using an Image block.
  4. Confirm “Could not finalize the upload.” appears.
  5. Confirm a DELETE /wp/v2/media/<id>?force=true request succeeds.
  6. Refresh the Media Library and confirm the failed attachment is absent.
  7. Disable request blocking and confirm a normal upload succeeds.

Screenshots or screencast

image

Use of AI Tools

Used OpenAI Codex to investigate the issue, add regression tests, and draft PR description.

@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

👋 Welcome

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @elphizu! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: elphizu <suryakantupadhyay@git.wordpress.org>
Co-authored-by: adamsilverstein <adamsilverstein@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

🏷️ Labels

This pull request needs exactly one label indicating its type, and has 0.

  • Required: any label starting with [Type].
  • Found: none.

Read more about Type labels in Gutenberg. If you cannot add labels yourself, a reviewer can do it for you.

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Oct 20, 2026
@elphizu
elphizu marked this pull request as ready for review October 21, 2026 05:25

This branch has not been deployed

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

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Upload Media: Delete the attachment when finalizing an upload fails

1 participant