Skip to content

Core Data: Fix deleting a single item that is fetched on its own with a query - #86408

Merged
andrewserong merged 2 commits into
trunkfrom
fix/core-data-remove-query-items-single-item
Nov 8, 2026
Merged

andrewserong merged 2 commits into
trunkfrom
fix/core-data-remove-query-items-single-item

Conversation

@andrewserong

Copy link
Copy Markdown
Contributor

What?

Fix a bug where deleting a single item that's fetched with its own query (e.g. adding _embed) throws an error.

This is effectively a follow-up to #76318 which updated core-data to treat single-item responses specially.

Why?

In receiveQueries we bail early for the case of a single item being received (that's the Array.isArray() check). However, this reducer provides a default state of {} so an empty entry gets stored for single items that contain a query.

As a result, further down the pipeline, when someone goes to delete the entity, removeQueryItems is called, and fails on the line const itemIds = queryItems.itemIds.filter because itemIds never exists.

At first, I was thinking of fixing this by doing an early return in removeQueryItems, but I think the better fix is to skip going into the receiveQueries reducer, as that's more in keeping with the spirit of #76318 (if I'm reading things correctly).

As for why we need to fix this? Well, over in #86349 I'm exploring adding actions to the experimental media editor route, so that we can enable permanently deleting media items. When I enabled the actions I ran into the bug that this PR fixes.

How?

  • In receiveQueries move the check that the action items is an array to before the reducer is called, rather than an early return within the reducer. This ensures that we're not left with a stray empty {} object

Testing Instructions

If you'd like to test this out manually, you can use the following from the dev console while you're in the block editor. On trunk it'll throw an error, and on this PR it'll succeed.

          ( async () => {
                  const { dispatch, resolveSelect, select } = wp.data;
                  const { id } = await dispatch( 'core' ).saveEntityRecord( 'postType', 'post', {
                          title: 'Delete me',
                          status: 'draft',
                  } );
                  await resolveSelect( 'core' ).getEntityRecord( 'postType', 'post', id, {
                          _embed: 'author',
                  } );
                  await dispatch( 'core' ).deleteEntityRecord( 'postType', 'post', id, {
                          force: true,
                  } );
                  console.log(
                          select( 'core' ).getLastEntityDeleteError( 'postType', 'post', id ) ??
                                  'Deleted without an error'
                  );
          } )();

Screenshots

Before this PR, you'd get the following error attempting to delete a single record that was received with a query (e.g. _embed: 'author'):

image

With this PR (if you're using the test script above) you should see this in the console:

image

Use of AI Tools

Claude Code (Opus 5.5)

…eleting it doesn't fail

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@andrewserong andrewserong self-assigned this Nov 6, 2026
@andrewserong andrewserong added [Type] Bug An existing feature does not function as intended [Package] Core data /packages/core-data labels Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 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: andrewserong <andrewserong@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

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

📦 Bundle size

Size Change: 0 B

Total Size: 8.61 MB

5b70665 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric f6e4282 trunk % Change
timeToFirstByte 47.3 ms +2.96% -4.44% 47.2 ms +8.47% -0.95% 0.21%
largestContentfulPaint 86 ms +2.33% -6.98% 84 ms +2.38% -4.76% 2.38%
lcpMinusTtfb 39.25 ms +2.04% -19.62% 32.9 ms +17.33% -6.84% 19.3%
wpBeforeTemplate 23.7 ms +3.12% -3.29% 23.87 ms +9.01% -0.92% -0.71%
wpTemplate 19.22 ms +4.58% -3.38% 19.55 ms +5.17% -1.02% -1.69%
wpTotal 43.59 ms +2.82% -5.02% 43.49 ms +8.85% -1.31% 0.23%
wpMemoryUsage 7.72 MB +0% -0% 7.68 MB +0% -0% 0.43%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric f6e4282 trunk % Change
timeToFirstByte 39.2 ms +2.42% -2.3% 39.6 ms +2.53% -3.16% -1.01%
largestContentfulPaint 88 ms +0% -0% 88 ms +4.55% -0% 0%
lcpMinusTtfb 49 ms +2.04% -1.33% 49.4 ms +2.53% -2.63% -0.81%
wpBeforeTemplate 20.59 ms +3.06% -0.68% 20.56 ms +2.04% -0.92% 0.15%
wpTemplate 15.13 ms +4.96% -3.57% 15.2 ms +8.22% -5.66% -0.46%
wpTotal 36.04 ms +2.94% -2.03% 36.3 ms +2.87% -2.53% -0.72%
wpMemoryUsage 6.41 MB +0% -0% 6.37 MB +0% -0% 0.53%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric f6e4282 trunk % Change
mediaProcessingJpeg 377.35 ms +2.77% -1.26% 395.77 ms +0.43% -4.28% -4.65%
mediaProcessingAvif 5515.26 ms +0.39% -0.06% 5610.27 ms +2.67% -0.45% -1.69%
mediaProcessingJpegToAvif 3763.21 ms +0.53% -0.51% 3799.74 ms +3.36% -0.42% -0.96%

media-upload

Metric f6e4282 trunk % Change
jpegUploadProcessing 1490.52 ms +31.99% -3% 1458.2 ms +1.8% -0.72% 2.22%
pngUploadProcessing 234.51 ms +4.13% -1.7% 235.31 ms +2.34% -2.67% -0.34%
largeJpegUploadProcessing 1446.6 ms +0.56% -0.51% 1446.2 ms +0.98% -0.65% 0.03%
multipleImageUploadProcessing 1574.56 ms +1.08% -1.17% 1571.14 ms +0.82% -1.06% 0.22%

post-editor

Metric f6e4282 trunk % Change
serverResponse 245.23 ms +7.21% -5.1% 246.73 ms +6.87% -5.31% -0.61%
firstPaint 293.63 ms +8.04% -25.05% 239.88 ms +24.87% -12.79% 22.41%
domContentLoaded 544.31 ms +1.98% -2.93% 549.56 ms +6.28% -1.22% -0.96%
loaded 544.99 ms +1.98% -2.88% 550.25 ms +6.26% -1.18% -0.96%
firstContentfulPaint 320.74 ms +3.04% -1.09% 333.19 ms +8.38% -1.97% -3.74%
firstBlock 1381.63 ms +0.97% -0.46% 1385.73 ms +2.31% -2.76% -0.3%
type 12.15 ms +5.35% -4.86% 12.04 ms +11.96% -7.89% 0.91%
typeWithoutInspector 12.09 ms +1.32% -9.26% 12.04 ms +4.07% -8.64% 0.42%
typeWithTopToolbar 15.78 ms +6.4% -4.06% 15.25 ms +2.23% -8.52% 3.48%
typeContainer 5.77 ms +4.16% -7.11% 6.07 ms +5.93% -11.04% -4.94%
focus 72.36 ms +4.35% -4.44% 69.21 ms +8.65% -4.02% 4.55%
firstFocus 145.35 ms +0% -0% 155.27 ms +0% -0% -6.39%
selectAll 247.11 ms +1.08% -0.46% 241.52 ms +0.29% -1.22% 2.31%
undo 21.37 ms +4.35% -3.51% 19.3 ms +8.34% -5.8% 10.73%
listViewOpen 45.5 ms +5.12% -2.26% 43.69 ms +11.1% -9.29% 4.14%
inserterOpen 28.82 ms +5.66% -7.29% 26.26 ms +4.11% -9.22% 9.75%
inserterHover 7.75 ms +8.9% -7.48% 7.44 ms +13.44% -8.06% 4.17%
inserterSearch 6.3 ms +2.38% -3.02% 6.32 ms +9.97% -6.49% -0.32%
loadPatterns 422.95 ms +2.11% -2.56% 424.32 ms +3.36% -2.97% -0.32%
wpTotal 238.37 ms +7.35% -5.29% 239.92 ms +7.08% -5.47% -0.65%
wpMemoryUsage 14.76 MB +0% -0% 14.73 MB +0% -0% 0.24%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric f6e4282 trunk % Change
serverResponse 330.91 ms +1.47% -9.6% 330.17 ms +0.18% -7.4% 0.22%
firstPaint 318.32 ms +10.36% -14.77% 332.84 ms +22.35% -2.63% -4.36%
domContentLoaded 749.02 ms +4.81% -2.08% 738.2 ms +2.11% -2.75% 1.47%
loaded 749.88 ms +4.8% -2.08% 739.1 ms +2.1% -2.75% 1.46%
firstContentfulPaint 439.69 ms +6.62% -1.93% 430.01 ms +4.34% -2.41% 2.25%
firstBlock 2152.96 ms +5.03% -0.66% 2145.55 ms +0.45% -1.7% 0.35%
type 16.75 ms +1.85% -8.6% 17.65 ms +3.29% -7.88% -5.1%
navigate 98.64 ms +1.5% -4.69% 103.65 ms +4.79% -5.38% -4.83%
loadPatterns 1109.41 ms +7.05% -6.56% 1220.8 ms +3.47% -13.23% -9.12%
loadPages 1008.79 ms +4.74% -4.2% 992.96 ms +2.27% -2.87% 1.59%
wpTotal 322.44 ms +1.51% -9.87% 321.69 ms +0.19% -7.61% 0.23%
wpMemoryUsage 12.78 MB +0% -0% 12.74 MB +0% -0% 0.27%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

5b70665 Run

🏁 Flaky tests

Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

Undo restores the post selection when another synced entity is loaded (RTC WebSocket) in /test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

  Object {
    "attributeKey": "content",
    "blockIndex": 0,
    "content": "abcdef",
-   "endOffset": 3,
-   "offset": 3,
+   "endOffset": 1,
+   "offset": 1,
  }

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts:363:5
moves selected text within a paragraph in /test/e2e/specs/editor/various/draggable-blocks.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 1
+ Received  + 1

  Array [
    Object {
      "attributes": Object {
-       "content": "second First third",
+       "content": "First second third",
      },
      "name": "core/paragraph",
    },
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/draggable-blocks.spec.js:1209:41
should give the image its context menu while selected @WebKit @Firefox in /test/e2e/specs/editor/blocks/image.spec.js, passed after 1 failed attempt.
Test timeout of 100000ms exceeded.
should select by dragging into nested block in /test/e2e/specs/editor/various/multi-block-selection.spec.js, passed after 2 failed attempts.
Error: expect(received).toEqual(expected) // deep equality

- Expected  - 1
+ Received  + 0

  Array [
    1,
-   2,
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/multi-block-selection.spec.js:569:5
Error: expect(received).toEqual(expected) // deep equality

- Expected  - 1
+ Received  + 0

  Array [
    1,
-   2,
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/multi-block-selection.spec.js:569:5
shows block preview when hovering over block in inserter in /test/e2e/specs/editor/various/inserting-blocks.spec.js, passed after 1 failed attempt.
Error: expect(locator).toBeInViewport() failed

Locator: locator('.block-editor-inserter__preview')
Expected: in viewport
Timeout: 5000ms
Error: element(s) not found

Call log:
  - Expect "toBeInViewport" locator('.block-editor-inserter__preview') with timeout 5000ms
  - waiting for locator('.block-editor-inserter__preview')

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/inserting-blocks.spec.js:819:5

5b70665 Run

@ramonjd ramonjd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'll give this a tentative check.

It's testing well for me.

I had to poke around, but the isArray check looks good in its new location.

Before #76318, receiveItems wrapped single items in an array (Array.isArray( items ) ? items : [ items ]), so they reached queries as a one-item list.

#76318 stopped wrapping them and added a check inside the reducer.

Returning early from there still stored the reducer's {} default, an entry with no itemIds.

Checking before the reducer runs means nothing gets stored at all.

Comment on lines +270 to +272
// `ifMatchingAction` lets through only collection responses.
action: ReceiveItemsAction &
Partial< QueryParts > & { items: Array< QueriedItem > }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This makes sense to me.

The Array.isArray( action.items ) seems like an oversight given the default state is {}?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, as far as I could tell, I think this might have just missed in the original PR. It's not that common to include a query with a single item and to then delete it via core-data, so it's probably just a flow we hadn't encountered until working on the media editor route.

@andrewserong

Copy link
Copy Markdown
Contributor Author

Thanks for the review @ramonjd! I'll leave this PR open until next week, so we can gather feedback from others in case there's other ways to handle this.

Between my own eyes and heavy reliance on a Claude session, I think this is a good way to fix it, but as always with core-data, I feel a bit cautious about changing things, so good to give others a chance to weigh in 🙂

@ramonjd

ramonjd commented Nov 6, 2026

Copy link
Copy Markdown
Member

as always with core-data, I feel a bit cautious about changing things

You and me both. Thanks for diving in

@Mamaduka Mamaduka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good. Thank you both!

@andrewserong
andrewserong merged commit 503ab9f into trunk Nov 8, 2026
108 checks passed
@andrewserong
andrewserong deleted the fix/core-data-remove-query-items-single-item branch November 8, 2026 22:08
@github-actions github-actions Bot added this to the Gutenberg 24.5 milestone Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Core data /packages/core-data [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants