Skip to content

clear(base) ignores the prefix in azure-storage-blob, azure-storage-table, mongodb and azure-cosmos drivers, deletes the entire store #802

Open

Description

@MILLERMARRU

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 null

Same 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() queries SELECT { id } from c (every item in the container) and deletes all of them
  • src/drivers/azure-storage-blob.ts: async clear() pages through listBlobsFlat() (no prefix) and deletes every blob in the container
  • src/drivers/azure-storage-table.ts: async clear() pages through listEntities() (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

  1. thribhuvan003 commented on Aug 16, 2026

    @thribhuvan003

    Tried to reproduce this on 2.0.0-alpha.7 before 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:

    root + prefixStorage("users").clear()  -> users:1 = "a", orders:1 = "b"
    storage.clear("users")                 -> users:1 = "a", orders:1 = "b"
    nested mount, clear("data:users")      -> users:1 = "a", orders:1 = "b"
    

    Nothing is deleted — the whole store survives, and so does the prefix.

    The reason looks like the difference between the two mount lookups. getKeys uses getMounts(base, true), so relativeBase can be non-empty and the driver really does receive a base (which is why the getKeys half of your analysis holds). clear uses getMounts(base, false), and that filter only keeps mountpoints that start with base — so mountpoint.length >= base.length, which makes base.length > mountpoint.length always false, and relativeBase comes out undefined every 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 under base. getKeys filters afterwards, clear has 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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions