Bulk deletion from a slice
A slice receives an action containing several IDs that should be deleted.
Redux Toolkit · Module
removeMany is a generated entity-adapter reducer that removes all entities matching a supplied array of IDs. Use it directly as a slice/reducer case or call it as an Immer helper inside reducer logic.
removeMany updates an entity adapter's normalized state by deleting each entity identified by the supplied ID array. It can be used directly as a case reducer or called as an Immer-backed mutating helper inside a reducer.
removeMany(state, keys: EntityId[]): EntityState<T>
removeMany(state, action: PayloadAction<EntityId[]>): EntityState<T>| Name | Type | Description |
|---|---|---|
state | EntityState<T> | The adapter-managed normalized entity state, or an Immer Draft of it when called manually. |
keys | EntityId[] | PayloadAction<EntityId[]> | The IDs of entities to remove; this is a plain array for helper use or a PayloadAction containing that array as payload for case-reducer use. |
The updated entity state, with matching entities removed.
createReducer or createSlice.import { createEntityAdapter, createSlice } from '@reduxjs/toolkit'
type Todo = { id: string; text: string }
const todosAdapter = createEntityAdapter<Todo>()
const todosSlice = createSlice({
name: 'todos',
initialState: todosAdapter.getInitialState(),
reducers: {
removeSelected: (state, action: { payload: string[] }) => {
todosAdapter.removeMany(state, action.payload)
},
},
})
export const { removeSelected } = todosSlice.actions
export default todosSlice.reducerRemoves every entity whose ID appears in the action payload.
A slice receives an action containing several IDs that should be deleted.
Reducer logic already has a list of IDs and needs to remove them in one adapter operation.
A normalized collection must remove selected records while keeping its adapter-managed state consistent.
removeMany do?removeMany is a generated entity-adapter reducer that accepts an array of entity IDs and removes matching entities from normalized state. It can be supplied directly as a case reducer or invoked inside another reducer.
Follow-up: How would you invoke it inside a slice reducer? Call adapter.removeMany(state, ids) where state is the reducer's Immer Draft and ids is the ID array.
The case-reducer form receives an action whose payload contains the IDs to delete, with the signature removeMany(state, keys: PayloadAction<EntityId[]>). The direct-helper form instead receives the ID array as its second argument.
Follow-up: What changes between the two forms? Only the input shape: an action payload for case-reducer use versus a plain ID array for a manual helper call.
It operates on an entity adapter's normalized entity state and checks each supplied ID. Existing matching entities are removed, while nonexistent IDs have no effect.
Follow-up: Why is normalized state useful here? The adapter maintains the generated ids and entities structures together, so callers do not manually coordinate both collections.
createEntityAdapterEntityStateEntityIdcreateReducercreateSliceremoveOneaddManyupsertMany