The clearRefinements widget provides a button to remove all active filters at once. It’s useful for helping users reset their search and start over.
Usage
const clearRefinements = instantsearch . widgets . clearRefinements ({
container: '#clear-refinements' ,
});
search . addWidget ( clearRefinements );
Examples
instantsearch . widgets . clearRefinements ({
container: '#clear-refinements' ,
});
With Included Attributes
instantsearch . widgets . clearRefinements ({
container: '#clear-filters' ,
includedAttributes: [ 'brand' , 'category' , 'price' ],
});
With Excluded Attributes
instantsearch . widgets . clearRefinements ({
container: '#clear-refinements' ,
excludedAttributes: [ 'query' ],
});
With Custom Template
instantsearch . widgets . clearRefinements ({
container: '#clear-refinements' ,
templates: {
resetLabel ({ hasRefinements }) {
return hasRefinements ? 'Clear all filters' : 'No filters applied' ;
},
},
});
Options
container
string | HTMLElement
required
CSS Selector or HTMLElement to insert the widget.
List of attributes to clear. When provided, only refinements from these attributes are cleared.
List of attributes to exclude from being cleared. Useful to preserve certain filters like the search query.
Function to transform the items before clearing. ( items : object []) => object []
Templates to customize the widget rendering. resetLabel
Template
default: "'Clear refinements'"
Template for the button label. Receives:
hasRefinements: Whether there are active refinements
CSS classes to add to the widget elements. CSS class for the root element.
CSS class for the button.
CSS class for the disabled button.
HTML Output
< div class = "ais-ClearRefinements" >
< button class = "ais-ClearRefinements-button" type = "button" >
Clear refinements
</ button >
</ div >
When disabled (no refinements):
< div class = "ais-ClearRefinements" >
< button
class = "ais-ClearRefinements-button ais-ClearRefinements-button--disabled"
type = "button"
disabled
>
Clear refinements
</ button >
</ div >
Use Cases
Clear All Except Query
instantsearch . widgets . clearRefinements ({
container: '#clear-filters' ,
excludedAttributes: [ 'query' ],
templates: {
resetLabel: 'Clear filters' ,
},
});
Clear Only Specific Filters
instantsearch . widgets . clearRefinements ({
container: '#clear-facets' ,
includedAttributes: [ 'brand' , 'category' , 'color' ],
templates: {
resetLabel: 'Clear facets' ,
},
});
With Custom Styling Based on State
instantsearch . widgets . clearRefinements ({
container: '#clear-refinements' ,
templates: {
resetLabel ({ hasRefinements }) {
return `
<span class=" ${ hasRefinements ? 'active' : 'inactive' } ">
${ hasRefinements ? '✕ Clear filters' : 'No active filters' }
</span>
` ;
},
},
});
Combine with currentRefinements
Often used together with the currentRefinements widget:
// Show current refinements
instantsearch . widgets . currentRefinements ({
container: '#current-refinements' ,
});
// Add clear all button
instantsearch . widgets . clearRefinements ({
container: '#clear-refinements' ,
});
Behavior
The clear button:
Is enabled when there are active refinements
Is disabled when there are no refinements to clear
Clears all refinements when clicked (or only those specified by includedAttributes/excludedAttributes)
Does not clear the search query by default (can be changed with includedAttributes)