Repository navigation
Description
Environment
- unstorage: 2.0.0-alpha.7
Reproduction
Any of the four drivers below, used through a prefix (either prefixStorage() or a nested mount whose relativeBase is non-empty):
import { createStorage, prefixStorage } from "unstorage";
import mongodb from "unstorage/drivers/mongodb";
const storage = createStorage({ driver: mongodb({ connectionString: "..." }) });
await storage.setItem("users:1", "a");
await storage.setItem("orders:1", "b");
const users = prefixStorage(storage, "users");
await users.clear();
console.log(await storage.getItem("orders:1")); // expected "b", actually nullSame reproduction shape works with azure-storage-blob, azure-storage-table and azure-cosmos.
Describe the bug
storage.clear(base) in src/storage.ts calls the driver's own clear directly with the resolved base when the driver defines one:
async clear(base, opts = {}) {
base = normalizeBaseKey(base);
await Promise.all(
getMounts(base, false).map(async (m) => {
if (m.driver.clear) {
return asyncCall(m.driver.clear, m.relativeBase, opts);
}
...There's no core-level scoping for clear (unlike getKeys, which is filtered afterward by filterKeyByBase). I read the source of all four drivers below and confirmed they define clear() with no parameters at all, so the base gets silently dropped and the whole underlying store is wiped instead of just the prefixed subset:
src/drivers/mongodb.ts:async clear() { await (await getMongoCollection()).deleteMany({}); }src/drivers/azure-cosmos.ts:async clear()queriesSELECT { id } from c(every item in the container) and deletes all of themsrc/drivers/azure-storage-blob.ts:async clear()pages throughlistBlobsFlat()(no prefix) and deletes every blob in the containersrc/drivers/azure-storage-table.ts:async clear()pages throughlistEntities()(no prefix) and deletes every entity in the table
getKeys() in these same four drivers also drops the base argument, but that's masked because storage.getKeys() filters the driver's result by base afterward (filterKeyByBase), so listing is still correct, just less efficient. clear() has no equivalent backstop, so calling it through a scoped view is a silent full-store wipe.
This is the same class of bug reported for the S3 driver in #728 and fixed in #760 (that report explicitly calls out clear("some-folder/") deleting the entire bucket as the dangerous half of the issue). I checked the other REST-backed drivers and they already do this correctly: netlify-blobs.ts passes { prefix: base } into its list/delete call, and cloudflare-r2-binding.ts/uploadthing.ts scope their delete to getKeys(base) first.
Suggested fix
Same pattern as the S3 fix: accept base in each driver's clear() and scope the list/query to it before deleting (Azure Blob/Table's list APIs take a prefix, Cosmos and Mongo need a query filter on the key prefix). Might also be worth a generic safety net in storage.clear() itself the way getKeys has one, so a driver bug like this degrades to "over-broad clear filtered down" instead of "clear does nothing scoped."
Activity
thribhuvan003 commentedon Aug 16, 2026
Tried to reproduce this on
2.0.0-alpha.7before working on a fix, and I get a different result — worth flagging before someone patches the drivers.All three shapes from the report, with the memory driver:
Nothing is deleted — the whole store survives, and so does the prefix.
The reason looks like the difference between the two mount lookups.
getKeysusesgetMounts(base, true), sorelativeBasecan be non-empty and the driver really does receive a base (which is why thegetKeyshalf of your analysis holds).clearusesgetMounts(base, false), and that filter only keeps mountpoints that start with base — somountpoint.length >= base.length, which makesbase.length > mountpoint.lengthalways false, andrelativeBasecomes outundefinedevery time.So the drivers'
clear()never actually receives a base. The missing parameter is real, but it looks latent rather than a live data-loss path.The part that does seem off is that
clear(base)quietly does nothing when no mount lives underbase.getKeysfilters afterwards,clearhas no equivalent, so a scoped clear just silently no-ops instead of clearing the prefix.Happy to be wrong if there's a setup where the driver does get a non-empty base — I only tried the three above. If the no-op is the actual bug here, I'd be glad to take it.