# What is the purpose of refreshApex, and how do you use it with wired data adapters?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, refreshApex, Caching

Read it on the site: https://www.forcechallenges.com/interview-questions/what-is-the-purpose-of-refreshapex-and-how-do-you-use-it-with-wired-data-adapters

## Answer

**Basics:** Wired Apex (`cacheable=true`) results are cached client-side for performance, so after you save something, the old list can still show. `refreshApex` tells the wire to get fresh data. It is imported from `@salesforce/apex`.

**How it works:**
1. Wire to a property or function and keep the entire provisioned object (not just `.data`).
2. After a mutation (imperative Apex DML, `updateRecord`, flow), call `refreshApex(this.wiredResult)`.
3. It returns a Promise; await it if you need to sequence actions.
4. The wire re-emits fresh `{data, error}` and the template re-renders.
5. When wiring to a function, store the whole argument in a field: `this.wiredResult = result`.

**Advanced / deep dive:** The argument must be the exact value the wire provisioned. Passing `result.data` does nothing. `refreshApex` only affects wired adapters of cacheable Apex; it does not affect results fetched imperatively, which are your own variables, so re-call the method instead. For Lightning Data Service adapters (`getRecord`) use `notifyRecordUpdateAvailable` or `getRecordNotifyChange` from `lightning/uiRecordApi` (the newer name replaced the older one in recent releases; verify in current release notes). LDS operations such as `updateRecord` refresh their own wires automatically, so you only need `refreshApex` when Apex changed the data. When multiple components wire the same Apex call with the same params they share one cache entry; one `refreshApex` call refreshes the data for all of them. Be careful with refresh loops where a handler triggers a refresh that triggers the handler. Cache refresh issues a server round trip, so avoid calling it per keystroke; debounce.

**Code / config example:**
```javascript
import { LightningElement, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getTasks from '@salesforce/apex/TaskController.getTasks';
import completeTask from '@salesforce/apex/TaskController.completeTask';

export default class TaskList extends LightningElement {
    wiredResult;
    tasks;
    @wire(getTasks)
    wired(result) {
        this.wiredResult = result;            // keep whole object
        if (result.data) this.tasks = result.data;
    }
    async handleComplete(e) {
        await completeTask({ taskId: e.target.dataset.id });
        await refreshApex(this.wiredResult);   // re-fetch from server
    }
}
```

**Hidden gotchas & must-remember points:**
- Pass the whole wire result, not `data`; passing `data` silently does nothing useful.
- It refreshes cacheable wired Apex only, not imperative calls.
- It is imported from `@salesforce/apex`, not from `lightning/uiRecordApi`.
- LDS adapters have their own refresh path via record notification functions.
- Records changed outside the component (another user) are not pushed; you must refresh or use events.
- Each call is a network request; do not call it in tight loops.

**Best practice:** Store the full wire result, refresh after confirmed writes, and prefer LDS operations when Apex is not truly needed.

---
Source: ForceChallenges · last updated 2026-11-02
