AI Security
Retaining and Deleting Agent Session Data, Uploaded Files, and Sandbox State
TL;DR: Keep agent session data only as long as it’s needed for the current interaction, store it encrypted, and delete it automatically after a short, configurable window. Uploaded files and sandbox state should be isolated per session, encrypted at rest, and purged immediately after the session ends unless a business reason exists. Use built‑in retention policies of your AI platform (e.g., Claude Managed Agents or OpenAI Agents) and supplement with simple scripts or workflow steps to enforce the rules.
What data does an AI agent keep during a session?
When you invoke a managed agent or a custom loop, the platform typically stores three categories of information:
- Conversation history: The series of prompts and model responses that form the dialogue.
- Uploaded artifacts: Files, images, or PDFs that the user attaches for processing.
- Sandbox state: Temporary variables, code execution results, or file system changes created by the agent’s internal sandbox.
All three are useful for continuity but also represent a privacy surface. If retained longer than necessary, they can expose proprietary data or personal information.
How long should session data be retained?
Guidelines from the OWASP GenAI Security Project recommend a “least‑privilege retention” approach: keep data only for the duration of the user’s task. In practice, small teams can adopt one of three windows:
- Ephemeral (seconds to minutes): For single‑turn queries where no follow‑up is expected. Delete immediately after the response is delivered.
- Short‑term (up to 24 hours): For multi‑turn workflows that may need a brief back‑and‑forth, such as a document‑summarization session.
- Business‑justified (days to weeks): When the output is stored for later review, e.g., a generated report that will be archived.
Choose the shortest window that still meets the user experience goal. Document the decision in a simple policy file (e.g., session_retention_policy.md) so new team members understand the rule.
Best practices for deleting uploaded files and sandbox state
Uploaded files are often the most sensitive payloads. Follow these steps:
- Isolate per session: Store each file in a dedicated temporary directory named with a random UUID. Do not reuse directories across sessions.
- Encrypt at rest: Use platform‑provided encryption (e.g., Cloudflare Workers AI encrypted KV storage) or encrypt manually with a key that rotates weekly.
- Immediate purge: After the session ends, invoke a cleanup routine that deletes the directory and any derived artifacts. In managed agents, set the
session_ttlparameter if available. - Audit logs: Record the file name hash, upload timestamp, and deletion timestamp in an immutable log (e.g., a write‑once log service or append‑only file).
Sandbox state follows the same pattern: keep it in memory when possible, otherwise write to an encrypted temporary store that is wiped on session termination.
Implementing automated retention policies
Most managed‑agent platforms expose configuration knobs. For example, Claude Managed Agents let you set session_expiration in the agent definition; OpenAI Agents support a max_history parameter that caps stored turns. If the platform lacks a built‑in TTL, add a lightweight wrapper:
async function runAgent(request) {
const sessionId = crypto.randomUUID();
const result = await agent.invoke(request, {sessionId});
// Schedule cleanup after 10 minutes
setTimeout(() => cleanupSession(sessionId), 10 * 60 * 1000);
return result;
}
In a no‑code tool like Zapier Agents or Make AI Agents, use the “Delay” step followed by a “Delete File” action to achieve the same effect without code.
Audit and compliance considerations
Even small teams may need to demonstrate that they handle data responsibly. Include these items in your compliance checklist:
- Retention policy documented and approved by a manager.
- Logs of every upload, access, and deletion event, stored for at least 30 days.
- Periodic review (monthly) of the cleanup scripts to ensure they still run after platform updates.
- Encryption keys rotated at least quarterly, with old keys archived securely.
When an audit request arrives, you can export the immutable log and show the timestamps that prove data was deleted as per policy.
“Treat AI session data like any other personal data: collect only what you need, keep it short, and delete it securely.” – OWASP GenAI Security Project
By embedding these controls into your workflow, you reduce the attack surface for prompt‑injection, data exfiltration, and insider misuse, while staying compliant with privacy expectations.
If you need help designing a retention pipeline that fits your stack, AISecAll offers a short‑term consulting service to audit your current AI agents and implement automated cleanup.
Need a practical AI security review?
AISecAll reviews prompts, tool permissions, document flows, and agent behavior so small teams can use AI without guessing where the risk sits.