Opened 2 months ago
Last modified 7 weeks ago
#65513 new defect (bug)
Media Library modal shows wrong count on first upload and does not reset after image deletion
| Reported by: | dilipbheda | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Media | Version: | |
| Severity: | normal | Keywords: | needs-test-info has-screenshots has-patch has-unit-tests |
| Cc: | Focuses: | administration |
Description
I found the following bugs in media uploads while user upload first time.
Bug 1: Wrong media count when uploading the first image
After uploading the first image through the Featured Image modal, the Media Library tab displays "Showing 1 of 2 media items" instead of "Showing 1 of 1 media items." The count is off by one on the initial upload.
Steps to reproduce:
Open any Page or Post in the editor.
Click "Set featured image" to open the Featured Image modal.
On the Upload files tab, upload one image (library was empty before).
Switch to the Media Library tab.
Expected: "Showing 1 of 1 media items"
Actual: "Showing 1 of 2 media items"
Bug 2: Modal does not reset after deleting the uploaded image
After clicking "Delete permanently" and confirming the deletion inside the Featured Image modal, the Attachment Details sidebar continues to display the deleted image's metadata (filename, date, file size, dimensions, Edit Image / Delete permanently links). The "Set featured image" button also remains active as if an item is still selected.
Steps to reproduce:
Open the Featured Image modal with at least one image in the library.
Select the image — Attachment Details populate in the right sidebar.
Click "Delete permanently" in the sidebar and confirm the browser dialog.
Expected: Attachment Details panel clears, selection state resets, "Set featured image" button becomes disabled.
Actual: Attachment Details panel still shows the deleted image's data. "Set featured image" button remains enabled.
Here is I attached record screen video for better understanding: https://tinyurl.com/26ocs6r4
Attachments (5)
Change History (12)
This ticket was mentioned in PR #12265 on WordPress/wordpress-develop by @khokansardar.
2 months ago
#1
- Keywords has-patch added; needs-patch removed
#2
@
8 weeks ago
I tested PR 12265 via WordPress Playground and found a mixed result.
Environment:
- WordPress: PR 12265 applied via WordPress Playground
- Browser: Firefox 152.0.1 (64-bit)
- OS: macOS Tahoe 26.5.1
Testing performed:
- Opened the Featured Image modal from the post editor.
- Uploaded one image into an initially empty Media Library.
- Confirmed the count showed “Showing 1 of 1 media items” instead of “Showing 1 of 2 media items.”
- Selected the uploaded image and used “Delete permanently” from the Attachment Details panel.
- Confirmed the browser delete confirmation dialog.
Result:
The first-upload count issue appears fixed in my testing.
However, after deleting the image permanently from the Featured Image modal, the deleted thumbnail still appeared in the modal even though the attachment appeared to be deleted. I was still able to select that stale thumbnail and click “Set featured image.” After doing so, the post editor showed “Could not retrieve the featured image data” in the Featured Image panel. Clicking “Remove” restored the expected empty state / “Set featured image” button.
So Bug 1 appears fixed, but the delete/reset behavior described in Bug 2 still appears reproducible in this Playground test.
#4
@
7 weeks ago
- Keywords needs-testing needs-patch removed
Hi there,
I tested this on WordPress Playground.
I verified both issues mentioned in the ticket:
For Bug 1, after uploading the first image through the Featured Image modal with an empty Media Library, the media count displayed "Showing 1 of 1 media items", which matches the expected behavior.
For Bug 2, after selecting the uploaded image and clicking Delete permanently, the attachment was removed successfully, the selection state was cleared, and the Featured Image modal behaved as expected.
Based on my testing, I was not able to reproduce either issue described in the ticket. The current behavior matches the expected results.
#5
@
7 weeks ago
Thanks @sanayasir. I dug into the conflicting results and it comes down to the test environment.
I tested the same steps in two places:
- WordPress Playground — could not reproduce either bug (matches your result). Video: https://www.awesomescreenshot.com/video/54171693?key=28e7a2c17659b59d4d4d45868550a661
- Local server (LocalWP) — could reproduce. Video: https://www.awesomescreenshot.com/video/54172102?key=f2ea33b7343ec12ec6e6c77cd23965e9
On the local server:
Bug 1 shows up exactly as reported — after uploading the first image into an empty library through the Featured Image modal, the count reads "Showing 1 of 2 media items" instead of "1 of 1".
Bug 2 also reproduces, though slightly differently than the original report. For me, the Attachment Details sidebar does clear after deleting, but the deleted image stays visible in the Media Library grid, and after closing the modal it's still shown as the featured image in the post editor. I confirmed this isn't a momentary glitch — the deleted image stays in the grid and doesn't clear on its own.
I think this explains the mixed results: on Playground the delete happens almost instantly, so everything looks clean, but on a real server the deleted image is left behind. The original report's "sidebar stays open" is likely just the brief moment while the delete is still processing, which is more noticeable on a slower/real server and invisible on Playground.
So these do look like genuine bugs rather than environment quirks — Bug 1 in particular needs a fix. I'll keep digging into the leftover-image behavior and follow up here. Will open a PR alongside.
This ticket was mentioned in PR #12392 on WordPress/wordpress-develop by @iamchitti.
7 weeks ago
#6
- Keywords has-patch has-unit-tests added
Trac ticket: https://core.trac.wordpress.org/ticket/65513
Fixes two issues in the media library modal (reproducible on a real server; masked on WordPress Playground because the delete request resolves almost instantly there).
### Bug 1 — wrong count on first upload
After uploading the first image into an empty library through the Featured Image modal, the Media Library tab read "Showing 1 of 2 media items" instead of "1 of 1".
Attachments.observe() bound the total-count handlers (_addToTotalAttachments / _removeFromTotalAttachments) to every observed collection. The Featured Image state also observes the selection (library.observe( this.get('selection') )), so with autoSelect an uploaded image is added to both the mirrored query and the selection, and was counted twice.
The counters are now bound only when the observed collection is the mirrored query (attachments === this.mirroring), so the total reflects the library itself. This also resolves the same latent double-count in the replace-image, gallery-edit, and collection-edit states.
### Bug 2 — deleted image lingers in the grid
After "Delete permanently" in the modal, the deleted attachment stayed visible in the Media Library grid (and remained the selected featured image), even though the query and global collections dropped it correctly.
"Delete permanently" destroys the model with { wait: true }, so Backbone fires the destroy event from its AJAX success callback — which runs before the delete request's own .done() handler, where this.destroyed = true was being set. During the destroy cascade the model therefore still validated as *not* destroyed, and the mirrored library collection re-added it via validate(). The flag is now set before the request (and restored on failure), so aggregate collections filter the attachment out the moment destroy fires. This is deterministic, not a race — the leftover item is stable until a re-query/reload.
### Also
- Corrects two inline-doc issues in the touched code: a missing
@paramname onobserve()and a copy/paste description on_removeFromTotalAttachments(). - Adds an end-to-end test (
tests/e2e/specs/featured-image-modal.test.js) covering both the count and the deletion. It passes with these changes and fails without them.
### Not included
A related symptom — after deleting the attachment, the block editor's sidebar still shows it as the featured image — lives in the block editor's data store (core/editor), whose source is in the Gutenberg repository rather than this one. Here it is - https://github.com/WordPress/gutenberg/issues/79820
### Testing
- Start with an empty Media Library.
- Edit a post, open the Featured Image modal, and upload one image on the Upload files tab.
- Switch to the Media Library tab → the count should read "Showing 1 of 1 media items".
- With the uploaded image selected, click Delete permanently and confirm → the image should disappear from the grid.
### Screenshots
## Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.8
Used for: Ticket analysis, root-cause investigation, and the end-to-end test. All changes were reviewed.
#7
@
7 weeks ago
I traced it and there are actually two separate things going on.
On the media modal side, the deleted image staying in the grid comes down to how the delete is handled — "Delete permanently" removes the attachment with wait: true, and Backbone fires the destroy event before the code that marks the model as deleted runs. So for a brief window the model still looks "alive" and the mirrored library collection re-adds it, which is why the thumbnail sticks around. Setting that flag before the request instead fixes it. Here is a PR https://github.com/WordPress/wordpress-develop/pull/12392 for this and the "1 of 2" count fix.
The other part — the post editor sidebar still showing the deleted image as the featured image after you close the modal — turned out to be in Gutenberg. That's the block editor's own data store, and its code is in the Gutenberg repo rather than core, so it can't be fixed in the same patch. Deleting the attachment does clean things up server-side (the thumbnail reference is removed), it's just that the open editor session isn't told about it until you reload. I've opened a Gutenberg issue to track that piece: https://github.com/WordPress/gutenberg/issues/79820
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)


Fixes the Media Library tab reporting "Showing 1 of 2 media items" after uploading the first image into an empty library through the Featured Image modal.
What the problem was:
wp.media.model.Attachments.observe()bound the total-attachments counters (_addToTotalAttachments/_removeFromTotalAttachments) to every observed collection.library.observe( this.get('selection') )). WithautoSelect, an uploaded image is added both to the mirrored query and to the selection, so it was counted twice — total became 2 for a single item.What the fix does:
@paramname onobserve()and a copy/paste description on_removeFromTotalAttachments()).Approach and why:
attachments === this.mirroringis the minimal, correct expression of that intent and also resolves the same latent double-count in replace-image, gallery-edit, and collection-edit.Trac ticket: https://core.trac.wordpress.org/ticket/65513
## Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.8
Used for: Ticket analysis, live reproduction, root-cause investigation and verification. All changes were reviewed, validated against the codebase, and are taken responsibility for by me (Khokan Sardar).