Skip to content

Add multi_delete() to delete multiple keys in one call - #1084

Open
ChrisJr404 wants to merge 1 commit into
aio-libs:masterfrom
ChrisJr404:multi-delete
Open

ChrisJr404 wants to merge 1 commit into
aio-libs:masterfrom
ChrisJr404:multi-delete

Conversation

@ChrisJr404

Copy link
Copy Markdown

This adds multi_delete() so you can delete a batch of keys in one call, rounding out the multi_get/multi_set pair. Closes #517.

Each backend does the sensible thing: valkey issues a single DEL with all the keys, memcached fires the deletes concurrently, and memory just pops them. Like delete(), it returns the number of keys that were actually removed, and it honours the namespace.

Added unit tests for each backend plus behavioural tests in the acceptance suite, along with a changelog entry and docs.

@greptile-apps

greptile-apps Bot commented Aug 25, 2026

Copy link
Copy Markdown

Confidence Score: 3/5

The PR should not merge until existing custom backends remain instantiable and memory batches correctly account for keys storing None.

The new abstract primitive creates an immediate compatibility failure for documented BaseCache extensions, while the memory implementation can remove a valid key yet return the wrong count and retain stale expiration state.

Files Needing Attention: aiocache/base.py, aiocache/backends/memory.py

Reviews (1): Last reviewed commit: "Add multi_delete to delete multiple keys..." | Re-trigger Greptile

Comment thread aiocache/base.py
Comment on lines +383 to +385
@abstractmethod
async def _multi_delete(self, keys, _conn=None):
raise NotImplementedError()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Abstract primitive breaks custom backends

When an existing custom BaseCache subclass lacks _multi_delete, making the new primitive abstract prevents that backend from being instantiated and raises TypeError, even when the application never calls multi_delete.

Suggested change
@abstractmethod
async def _multi_delete(self, keys, _conn=None):
raise NotImplementedError()
async def _multi_delete(self, keys, _conn=None):
deleted = [await self._delete(key, _conn=_conn) for key in keys]
return sum(deleted)

Knowledge Base Used:

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment on lines +126 to +127
async def _multi_delete(self, keys, _conn=None):
return sum(self.__delete(key) for key in keys)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 None values corrupt deletion accounting

When a memory-cache key stores literal None, _multi_delete removes the key but reports zero deletions because __delete treats the popped value as a miss; when that key has a TTL, its expiration handle also remains registered until it fires.

Knowledge Base Used:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that a bug in __delete()? i.e. Should be fixed separately, not related to this PR.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. The None value issue is a pre-existing bug in __delete() — it predates this PR and affects _delete() equally. _multi_delete is correctly delegating to the same __delete method, so it's consistent with the existing behavior.

This PR shouldn't be held responsible for fixing a separate bug. Happy to withdraw that part of the review.

Tip: You can customize Greptile's behavior for this repo with .greptile/rules.md and .greptile/config.json.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aiolibsbot implement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

implement multi_delete

2 participants