You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add .clear() to remove all member controls of FormGroup|FormRecord #50748
Which @angular/* package(s) are relevant/related to the feature request?
No response
Description
FormArray has its convenience method FormArray.clear to help with swiftly removing all controls. FormRecord and FormGroup should have theirs too.
Use case
When building a set of checkboxes, besides using FormArray for this, one can also use a FormGroup|FormRecord approach, where the set has a value in shape of an object:
When building the form record out of an array, you'd have code along these lines somewhere:
buildOptionsRecord(availableOptions: Option[]): void{// before constructing the FormRecord you need to first empty it// instead of iterating over the members and having to `.removeControl()` by name// I'd like to be able to say this.availableOptionsRecord.clear()// and have it emptied in one go// now build the controlsavailableOptions.forEach(option=>{this.availableOptionsRecord.addControl(option.id,false)});}
In response to the incoming array of available options you would need to first empty the current FormGroup|FormRecord. Currently this requires a very clumsy while loop.
Reacted by Emmanuel Roux, veso0308, Ahmed Jab, Rick Trevino, Ivan Kovarin, hedgybeats, Kevin Cheng, Jean-Christophe Coleau, Loïc Juillet, Bruno C. and 8 more
But for consistency with existing api I think it can make sense, because FormGroup already has methods like setControl and removeControl.
EDIT: I just realize this may cause some trouble: in strongly-typed forms, removeControl doesn't allow removing required controls. This kind of type-check would be impossible (or very complicated) with a method like clearControls...
I have a case of dynamically filled form of type FormGroup<Record<string, FormControl<string>>
and Object.keys(form.controls).forEach(key => form.removeControl(name)) shows typescript error that name should be of type never. Basically I also need to clear all controls from form and to refill it with new controls. this is more convenient than form recreation because there is form.valueChanged subscription on component init
use case: dynamic string variables filling, variables are loaded on other field change
Reacted by Loïc Juillet, Dmitrii Kuzmin, Dominik Linkowski, David Šánek, Mateusz, digeomel and Lani Moo
@JeanMeche why doesn't it make sense for FormGroups? I don't get it, I have this exact need right now and I have to iterate over each control key to remove it.
@JeanMeche this can be closed. It is new public API on FormGroup and FormRecord in a package that is only maintained now, which is the same reason #49098 asked for clearControls and #12747 was closed before that.
The loop already works, and with {emitEvent: false} on each removal plus one updateValueAndValidity() at the end it is a single update rather than N.
needsTriage since 2023, no duplicates. Signal forms removes the pattern: replacing the model value replaces the fields, so there is nothing to clear first.
Activity
JeanMeche commentedon Jun 17, 2023
While it makes sense for an
FormArray, it doesn't forFormGroup. The keys are specified at creation.Maybe just for
FormRecordthen ?EmmanuelRoux commentedon Jun 17, 2023
I agree with you @JeanMeche
But for consistency with existing api I think it can make sense, because
FormGroupalready has methods likesetControlandremoveControl.EDIT: I just realize this may cause some trouble: in strongly-typed forms,
removeControldoesn't allow removing required controls. This kind of type-check would be impossible (or very complicated) with a method likeclearControls...blazkovicz commentedon Nov 23, 2023
I have a case of dynamically filled form of type
FormGroup<Record<string, FormControl<string>>and
Object.keys(form.controls).forEach(key => form.removeControl(name))shows typescript error that name should be of type never. Basically I also need to clear all controls from form and to refill it with new controls. this is more convenient than form recreation because there is form.valueChanged subscription on component inituse case: dynamic string variables filling, variables are loaded on other field change
digeomel commentedon Jul 2, 2024
@JeanMeche why doesn't it make sense for FormGroups? I don't get it, I have this exact need right now and I have to iterate over each control key to remove it.
JeanMeche commentedon Jul 2, 2024
Groups have defined keys in the type, clear would remove those keys, breaking thus breaking the typings.
digeomel commentedon Jul 2, 2024
How is this any different?
Object.keys(myFormgroup.controls).forEach(key => myFormgroup.removeControl(key));JeanMeche commentedon Jul 2, 2024
https://v17.angular.io/api/forms/FormGroup#removecontrol
What you really want in a typed context are FormRecords, which should land soon a clear() method, see #50750
PapaNappa commentedon Sep 17, 2025
#50750 seems to have been un-shipped and closed.
Still the changelog for 19.0.0 says
Is this still on the radar?
arturovt commentedon Sep 29, 2026
@JeanMeche this can be closed. It is new public API on FormGroup and FormRecord in a package that is only maintained now, which is the same reason #49098 asked for clearControls and #12747 was closed before that.
The loop already works, and with {emitEvent: false} on each removal plus one updateValueAndValidity() at the end it is a single update rather than N.
needsTriage since 2023, no duplicates. Signal forms removes the pattern: replacing the model value replaces the fields, so there is nothing to clear first.