{"id":"23f067e76f113559","repo":"gofiber/fiber","slug":"cache-failed-to-delete-key-q-w","errorCode":null,"errorMessage":"cache: failed to delete key %q: %w","messagePattern":"cache: failed to delete key %q: %w","errorType":"http","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"middleware/cache/manager.go","lineNumber":201,"sourceCode":"\n// set data to storage or memory\nfunc (m *manager) setRaw(ctx context.Context, key string, raw []byte, exp time.Duration) error {\n\tif m.storage != nil {\n\t\tif err := m.storage.SetWithContext(ctx, key, raw, exp); err != nil {\n\t\t\treturn fmt.Errorf(\"cache: failed to store raw key %q: %w\", m.logKey(key), err)\n\t\t}\n\t\treturn nil\n\t}\n\n\tm.memory.Set(key, raw, exp)\n\treturn nil\n}\n\n// delete data from storage or memory\nfunc (m *manager) del(ctx context.Context, key string) error {\n\tif m.storage != nil {\n\t\tif err := m.storage.DeleteWithContext(ctx, key); err != nil {\n\t\t\treturn fmt.Errorf(\"cache: failed to delete key %q: %w\", m.logKey(key), err)\n\t\t}\n\t\treturn nil\n\t}\n\n\tm.memory.Delete(key)\n\treturn nil\n}\n\nfunc (m *manager) logKey(key string) string {\n\tif m.shouldRedactKeys {\n\t\treturn redactedKey\n\t}\n\treturn key\n}\n","sourceCodeStart":183,"sourceCodeEnd":216,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/middleware/cache/manager.go#L183-L216","documentation":"Thrown at middleware/cache/manager.go:201 by manager.del() when storage.DeleteWithContext fails. The cache middleware deletes keys during invalidation (e.g. when a cached response is stale beyond revalidation, or on explicit purge); a failure here means the storage backend rejected the DELETE.","triggerScenarios":"Cache invalidation path runs while Storage is unreachable, the connection timed out, or the request context was cancelled before the DELETE completed.","commonSituations":"Storage backend briefly down during an invalidate call; ctx cancellation; Redis in a read-only replica mode; ACL/permission mismatch where the app user lacks DELETE permission.","solutions":["Check the wrapped error for the backend-specific cause and fix it (connectivity, ACLs, timeout).","Grant the storage user DELETE permission if it was scoped to read/write only.","Tune the storage driver timeout so deletes complete within the request lifetime.","Make cache invalidation best-effort: log the failure and let entries expire via TTL instead of failing the user request.","Verify storage connectivity with a health probe on a regular schedule."],"exampleFix":"// before: invalidate failure bubbles up as 500\n// (default error handler)\n\n// after: log + degrade; TTL still bounds staleness\norigHandler := app.ErrorHandler\napp.ErrorHandler = func(c fiber.Ctx, err error) error {\n    if strings.HasPrefix(err.Error(), \"cache: failed to delete\") {\n        log.Printf(\"cache delete failed (TTL will reclaim): %v\", err)\n        return c.Next()\n    }\n    return origHandler(c, err)\n}","handlingStrategy":"fallback","validationCode":"// Confirm DELETE works at boot (some ACLs grant SET but not DELETE).\nctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)\ndefer cancel()\n_ = store.SetWithContext(ctx, \"__del_probe__\", []byte(\"x\"), time.Minute)\nif err := store.DeleteWithContext(ctx, \"__del_probe__\"); err != nil {\n    log.Fatalf(\"cache storage DELETE denied: %v\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := m.storage.DeleteWithContext(ctx, key); err != nil {\n    // Best-effort: TTL will reclaim the entry anyway.\n    log.Printf(\"cache delete failed for %q (TTL reclaims): %v\", key, err)\n    return nil\n}","preventionTips":["Grant the storage user DELETE permission alongside GET/SET.","Make invalidation best-effort; rely on TTL as the safety net.","Avoid pointing the cache at read-only storage replicas.","Monitor delete-error rates; they often reveal failover or ACL drift."],"tags":["cache","storage","network","fiber"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}