NuxtN
Nuxt12mo ago
5 replies
RicharDVD

Clear nitro route cache programatically

Hi! I would like to clear some cache programatically in a Nitro handler so another service can call this route to clear a certain part of the cache. I currently have the following code:

export default defineEventHandler(async () => {
  try {
    const storage = useStorage();
    // Clear the brands cache
    await storage.clear('cache:nitro:handlers:car-brands');
    const keys = await storage.keys('cache:nitro:handlers:car-brands');

    return {
      keys,
    };
  } catch (error) {
    console.error('Failed to clear cache:', error);
    throw createError({
      statusCode: 500,
      message: 'Failed to clear cache',
    });
  }
});

I fetch the keys for debugging purposes. When fetching the keys, I do receive the correct keys that are cached but the clear method doesn't seem to do much? When I look in the nitro cache folder locally, the files are still there.
{
  "keys": [
    "cache:nitro:handlers:car-brands:carbrands.json",
    "cache:nitro:handlers:car-brands:carmodelsMn2yMn29.json",
    "cache:nitro:handlers:car-brands:carmodelsOJopAqe7.json"
  ]
}


What am I doing wrong?
Was this page helpful?