sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[FEATURE] Add `delete_memory` / `forget_memory` tool to MemoryManager

Open ctoh-skechers opened this issue 4 months ago • 5 comments

SDK Language

Both / Not language-specific

Problem Statement

The MemoryManager currently provides search_memory and add_memory tools for agents, but there is no way for an agent to delete or forget previously stored memories. Once a memory is written to a writable store (e.g., BedrockKnowledgeBaseStore), it persists indefinitely with no SDK-level mechanism to remove it.

This creates problems when:

  • A user asks the agent to forget something they previously shared
  • Memories become stale or incorrect and need to be removed
  • Users want control over what personal data the agent retains (privacy/GDPR considerations)
  • Extracted memories contain errors that the agent should be able to correct by deletion

Proposed Solution

Add a delete_memory tool (or forget_memory) to the MemoryManager that:

  1. Accepts a memory_id or query parameter to identify which memory to delete
  2. Operates on writable stores only (similar to add_memory)
  3. Delegates deletion to the store's backend (e.g., removing the S3 object and triggering a KB re-sync for BedrockKnowledgeBaseStore)

Example interface:

# Agent-invocable tool
def delete_memory(memory_id: str, store: str | None = None) -> str:
    """Delete a specific memory from a writable store."""
    ...

The store interface would need a delete method:

class BedrockKnowledgeBaseStore:
    def delete(self, memory_id: str) -> bool:
        # Remove from S3 data source
        # Trigger ingestion job to re-sync
        ...

A MemoryDeleteToolConfig (similar to MemoryAddToolConfig) could control whether the tool is exposed to the agent.

Use Case

Use Case:

  1. User-requested forgetting: A user tells the agent "Forget that my favorite color is blue" — the agent should be able to remove that specific memory from the user's store.

  2. Privacy compliance: Users request deletion of personal data. The agent can act on this immediately rather than requiring manual admin intervention.

  3. Memory correction: Rather than adding a contradicting memory ("Actually my favorite color is NOT blue"), the agent can cleanly delete the incorrect one and optionally store the correction.

  4. Session cleanup: An agent that stores temporary context as memories during a long task can clean up after itself.

Alternatives Solutions

Alternative Solutions:

  1. Overwrite with contradicting memory — The agent writes a new memory that supersedes the old one. Pros: works today. Cons: accumulates stale data, relies on retrieval ranking to surface the latest, no actual deletion.

  2. Custom @tool wrapping S3/KB APIs — Build a custom delete tool outside the SDK that directly deletes from the backing store and triggers re-sync. Pros: works today. Cons: tightly couples to the store implementation, duplicates logic the SDK should own, different for every store backend.

  3. Admin API / out-of-band deletion — Expose an HTTP endpoint for users to manage their memories outside the agent. Pros: gives users direct control. Cons: not agent-driven, requires separate UI/UX work, doesn't solve the in-conversation "forget this" use case.

Additional Context

Additional Context:

The search_memory tool already returns results that could include a memory_id or similar identifier. This identifier could be passed to delete_memory to target a specific entry.

For BedrockKnowledgeBaseStore backed by S3, deletion would involve:

  1. Identifying the S3 object (by memory ID / key)
  2. Calling s3:DeleteObject
  3. Triggering bedrock-agent:StartIngestionJob to re-sync the KB

The add_memory flow already handles writing to S3 and syncing, so the inverse operation should follow the same pattern.

ctoh-skechers avatar Jul 30 '26 20:07 ctoh-skechers

Opened a draft PR that implements this: #4106.

It adds an opt-in delete_tool_config on MemoryManager, a programmatic MemoryManager.delete(memory_id), and an agent-invocable delete_memory tool. search_memory results now carry a memory_id that can be passed back to delete_memory. MemoryStore gains an optional delete(memory_id) method (implemented for FileMemoryStore); other stores like BedrockKnowledgeBaseStore can implement it in follow-ups using the S3-delete + re-sync flow described in the issue.

I use a coding assistant to help implement, and I review and take responsibility for the final change.

lifelmy avatar Sep 01 '26 14:09 lifelmy

Thanks for the contribution! Closing out since memory deletion should be done by overwrite for now/this is not in scope.

lizradway avatar Sep 04 '26 20:09 lizradway

Thanks for the contribution! Closing out since memory deletion should be done by overwrite for now/this is not in scope.

Does the current implementation allow overwriting existing memory? @lizradway

ctoh-skechers avatar Sep 04 '26 20:09 ctoh-skechers

Thanks for the contribution! Closing out since memory deletion should be done by overwrite for now/this is not in scope.

Does the current implementation allow overwriting existing memory? @lizradway

@ctoh-skechers which memory store backend are you using?

For Bedrock KB, I'd suggest using the native Bedrock API.

I'm not sure if we'd want to expose this top level on Memory Manager yet. I think there definitely could be a use case though for exposing this tool on the Bedrock KB cc: @opieter-aws what do you think?

lizradway avatar Sep 04 '26 20:09 lizradway

@lizradway Am using Bedrock KBs, backed with S3. In testing the current implementation will write to managed KB and S3 just fine. However, based on what your suggesting for deletes/overwrites would we need to trigger deletions on the Managed KB? Or would we need to delete in the S3 bucket itself and then trigger another resync on the KB? That would be two separate API calls.

I think based on the documentation it's the latter. Would be nice if the Strands SDK handled that, but I can understand the hesitancy there and requiring users to manage delete/put S3 API calls and KB resyncs themselves.

Terraform for the KB resource is like this

module user-memory {
    source = "terraform-aws-modules/s3-bucket/aws"
    version = "5.10.0"

    bucket = "${name}"
}

resource aws_bedrockagent_data_source user-memory {
    knowledge_base_id = aws_bedrockagent_knowledge_base.user-memory.id
    name              = "${local.prefix}-data-source"

    data_source_configuration {
        type = "MANAGED_KNOWLEDGE_BASE_CONNECTOR"

        managed_knowledge_base_connector_configuration {
            connector_parameters = jsonencode({
                type    = "S3"
                version = "1"
                connectionConfiguration = {
                    bucketName           = module.user-memory.s3_bucket_id
                    bucketOwnerAccountId = data.aws_caller_identity.current.account_id
                }
                aclEnabled = false
                filterConfiguration = {
                    maxFileSizeInMegaBytes = "500"
                }
            })

            media_extraction_configuration {
                image_extraction_configuration {
                    image_extraction_status = "ENABLED"
                }
            }
        }
    }

ctoh-skechers avatar Sep 08 '26 15:09 ctoh-skechers