TIL

How Wagtail Removes StreamField Blocks

Removing a block from a Wagtail StreamField in the editor does not remove it from the page. The block is hidden, but its fields stay in the form, keep their values, and are submitted on save. The server discards it later. This rarely matters until client-side logic starts reading the form.

In the Browser

When an editor deletes a block, Wagtail hides its container, sets aria-hidden="true" on it, and sets the block's hidden -deleted input to 1. Every other input belonging to the block stays in place. Until the page is saved, the removed block is still a complete set of form fields with its original values.

On Save

A StreamField is a single JSON column on the page, and each revision stores a full JSON copy of the page's content. Nothing is deleted row by row. On save, StreamBlock.value_from_datadict loops over every child up to -count, skips any marked -deleted, sorts the rest by -order, and builds a new StreamValue. ListBlock works the same way. The rebuilt value is written in full, and the removed block is simply absent from the new JSON.

Why It Matters

This explained a bug in CSS :has() rules that toggled editor panels based on the audience selected in condition blocks. Removed conditions still matched, because their <select> elements were in the DOM with their values intact. Scoping the selectors to children without aria-hidden="true" resolved it. The same applies to JavaScript that reads StreamField values: check the block's -deleted input or hidden state first.

Conclusion: A removed StreamField block remains in the form until save, and Wagtail drops it only when rebuilding the stream on the server. Client-side logic that reads the form has to filter out deleted blocks itself.