D365FO temp table: correct lifecycle for setTmpData/clearTmpData/deleteTmpData across method boundaries
In Dynamics 365 Finance and Operations, what are the correct usage patterns and lifecycle guarantees for setTmpData(), clearTmpData(), and deleteTmpData() when working with TempDB/in-memory temp table buffers?
Specifically, I’m seeing temp records either unexpectedly persist or disappear when I:
- populate a temp table in one method, pass the temp table buffer to another service method (or return it from a method), and then continue processing
- switch between running in the same request vs. batch/job context
- reuse the same temp table variable across multiple calls
How should I structure the code so that the temp data is reliably available when needed, and properly cleared/deleted when no longer required (including what actually happens to the underlying tmpId / TempDB records for each call)?
1 answer
Write an answerShort answer#
For TempDB/in-memory temp tables in D365FO, treat setTmpData() / clearTmpData() / deleteTmpData() as buffer-to-buffer lifecycle controls, not as “ownership transfer” across arbitrary method boundaries.
Reliable pattern: create/populate a temp table in the method (or in the caller), pass the buffer only while the same caller continues using it, and then explicitly clear/delete in the same logical owner scope. If you need data in another execution context (service call is still in-process vs. another thread/worker/batch rerun), don’t rely on temp table state; instead materialize to real tables or pass a serialized DTO / key list.
What these APIs practically do (and why you see “persist” or “disappear”)#
In D365FO, a Tmp table buffer can represent different backing storage modes:
- In-memory (fast, per-process)
- TempDB (persists for the duration of the session/request semantics that the kernel ties to temp scope)
setTmpData() is essentially “attach this buffer to its temp backing store for the lifetime that the kernel associates with that temp instance.”
- If you reuse a temp table variable instance, and/or
- if you move/copy the buffer between methods incorrectly (e.g., calling
deleteTmpData()/clearTmpData()elsewhere while another caller still expects it), and/or - if the code path differs between synchronous request and batch/job (different temp scope lifecycle)
…then your temp records can appear to either:
- persist longer than expected (because the backing store remains for the scope), or
- disappear early (because temp backing got detached/cleared/deleted, or because you relied on in-memory state in a context where it doesn’t survive).
Important: you should assume no guarantee that a temp backing store is automatically preserved across: - asynchronous boundaries - separate job phases - separate kernel sessions
TempDB lifetime is tied to the temp scope the kernel creates; don’t treat it as “global temp storage”.
Recommended coding patterns#
Pattern A (recommended): Populate → use → clear/delete in the same logical owner#
Do not rely on side effects across services. Make the caller the owner of the temp data.
// Caller
public void run()
{
MyTmpTable tmp;
// IMPORTANT: attach temp backing explicitly when you intend TempDB mode
tmp.setTmpData();
try
{
this.populateTmp(tmp);
// Pass buffer by reference (same call stack / same execution)
this.doWorkUsingTmp(tmp);
// If you must return it, consider returning *keys* or a DTO
// unless you fully control the lifetime.
}
finally
{
// Explicitly clear/delete once owner scope is done.
// Use ONE approach consistently; see below.
tmp.clearTmpData();
// or tmp.deleteTmpData();
}
}
private void populateTmp(MyTmpTable _tmp)
{
// Fill _tmp
}
private void doWorkUsingTmp(MyTmpTable _tmp)
{
// Read from _tmp
}Why this works: you establish a clear lifecycle boundary. You don’t “strand” temp backing stores, and you don’t detach them while other methods still need them.
Pattern B: If you reuse the same temp variable multiple times, reset before repopulating#
If you reuse the same temp table variable instance in a loop, always reset the underlying temp backing store before repopulation.
MyTmpTable tmp;
for (int i = 1; i <= 10; i++)
{
// Ensure clean state for this iteration
if (tmp.tmpDataIsSet()) // pseudo; see note below
{
tmp.clearTmpData();
// or tmp.deleteTmpData();
}
else
{
tmp.setTmpData();
}
this.populateTmp(tmp, i);
this.doWorkUsingTmp(tmp);
}Note: I’m not asserting the existence of tmpDataIsSet()—use whatever check you already use in your codebase, or simply do tmp.deleteTmpData(); tmp.setTmpData(); deterministically.
clearTmpData() vs deleteTmpData() (choose deliberately)#
clearTmpData(): keeps the temp “identity/backing” and just removes records. Good when you will repopulate and want to avoid churn.deleteTmpData(): removes the temp backing/records and effectively ends the temp data for that temp instance.
Practical guidance:
- If the temp table instance will be reused in the same owner scope: prefer
clearTmpData(). - If the temp table instance will not be used again (especially before returning from a service boundary you don’t fully control): prefer
deleteTmpData()in the owner’sfinally.
Edge case: If method A populates temp and calls deleteTmpData() before method B finishes using it, method B may observe an empty set.
Passing temp buffers to other methods/services#
Safe when:#
- The other method runs in the same call stack (same request execution)
- The temp backing isn’t cleared/deleted by either side until all consumers finish
Not safe when:#
- You cross execution context boundaries (e.g., asynchronous processing, batch stages that can’t share the same temp scope)
- You return the temp buffer to a caller that uses it after the owner
finallyblock already ran
Rule: If you return temp data from a method, document/guarantee who owns cleanup and ensure you don’t cleanup in the callee if the caller expects to use it later.
“tmpId / TempDB records for each call” — what to expect#
D365FO temp tables use an internal temp identity (“tmpId”) that the kernel ties to the temp backing. In practice:
- A single temp table buffer instance with
setTmpData()attached has a tmp identity tied to that instance. - Calling
clearTmpData()removes rows but keeps that identity alive for repopulation. - Calling
deleteTmpData()discards it.
Therefore, if you “expect” data from call #1 to still be there in call #2 but you used deleteTmpData() (or the buffer was cleared elsewhere), you’ll see disappearance.
If you “expect” data to be gone after the method ends but you didn’t delete/clear (and the kernel keeps it for the temp scope), you may see persistence within the same request/job.
Batch/job vs same request#
The main issue is that TempDB/in-memory temp lifetimes differ with execution context.
Pattern for reliability in batch:
- Do not rely on temp data surviving between batch stages.
- Treat each batch step as a fresh owner scope: populate temp, process immediately, then
clearTmpData()/deleteTmpData()in that step.
If you need to carry data across batch boundaries, persist it to:
- real tables, or
- a durable temp mechanism you control (e.g., staging tables keyed by an execution id)
Checklist (what to do in your code)#
- Always establish ownership: decide which method is responsible for cleanup.
- In the owner method, wrap use with
try { ... } finally { clearTmpData()/deleteTmpData(); }. - If reusing the same temp variable instance: clear/delete before repopulating.
- Never
deleteTmpData()while another consumer might still read the temp. - For any boundary that might change execution context (batch stages, async), don’t pass temp table buffers expecting continuity; pass keys/DTOs and repopulate, or persist.
If you want, paste your snippet#
If you share the relevant code (especially where setTmpData() is called and where clearTmpData()/deleteTmpData() happens), I can point out the exact lifecycle bug pattern (e.g., cleanup in callee, buffer reuse without clear, or relying on in-memory mode when the service expects TempDB).
Know the solution?
Sign in to contribute an answer and build your community reputation.
Sign in to answer