Skip to content

Add .clear() to remove all member controls of FormGroup|FormRecord #50748

Closed as not planned

Description

@moniuch

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:

this.fb.record({
   [ key: string ]: this.fb.control(false | true)
});

so that the value becomes very natural to read and understand and to set:

hasPrivateBathroom: true,
hasTv: false,
hasBalcony: true,
hasMinibar: true,

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 controls
    availableOptions.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.

Proposed solution

add FormGroup#clear() as well FormRecord#clear()

Alternatives considered

while(Object.keys(formGroup.controls).length){
      const toRemove = Object.keys(formGroup.controls)[0];
      formGroup.removeControl(toRemove)
}

No one want to have to write this type of code.

Activity

  1. JeanMeche commented on Jun 17, 2023

    @JeanMeche
    Member

    While it makes sense for an FormArray, it doesn't for FormGroup. The keys are specified at creation.

    Maybe just for FormRecord then ?

  2. added
    forms: Controls API
    on Jun 17, 2023
  3. added this to the needsTriage milestone on Jun 17, 2023
  4. EmmanuelRoux commented on Jun 17, 2023

    @EmmanuelRoux
    Contributor

    I agree with you @JeanMeche

    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...

  5. blazkovicz commented on Nov 23, 2023

    @blazkovicz

    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

  6. digeomel commented on Jul 2, 2024

    @digeomel

    @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.

  7. JeanMeche commented on Jul 2, 2024

    @JeanMeche
    Member

    Groups have defined keys in the type, clear would remove those keys, breaking thus breaking the typings.

  8. digeomel commented on Jul 2, 2024

    @digeomel

    How is this any different?
    Object.keys(myFormgroup.controls).forEach(key => myFormgroup.removeControl(key));

  9. JeanMeche commented on Jul 2, 2024

    @JeanMeche
    Member
    Screenshot 2024-07-02 at 21 24 42

    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

  10. PapaNappa commented on Sep 17, 2025

    @PapaNappa

    #50750 seems to have been un-shipped and closed.
    Still the changelog for 19.0.0 says

    add ability to clear a FormRecord (#50750)

    Is this still on the radar?

  11. arturovt commented on Sep 29, 2026

    @arturovt
    Contributor

    @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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: formsforms: Controls API

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions