{"record":{"id":"1986a3c068e3bc18","repo":"micro/go-micro","slug":"failed-to-delete-bucket","errorCode":null,"errorMessage":"Failed to delete bucket","messagePattern":"Failed to delete bucket","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"store/nats-js-kv/nats.go","lineNumber":272,"sourceCode":"\topt := store.DeleteOptions{}\n\n\tfor _, o := range opts {\n\t\to(&opt)\n\t}\n\n\tif opt.Database == \"\" {\n\t\topt.Database = n.opts.Database\n\t}\n\n\tif opt.Table == \"\" {\n\t\topt.Table = n.opts.Table\n\t}\n\n\tif opt.Table == \"DELETE_BUCKET\" {\n\t\tn.buckets.Del(key)\n\n\t\tif err := n.js.DeleteKeyValue(key); err != nil {\n\t\t\treturn errors.Wrap(err, \"Failed to delete bucket\")\n\t\t}\n\n\t\treturn nil\n\t}\n\n\tstore, ok := n.buckets.Get(opt.Database)\n\tif !ok {\n\t\treturn ErrBucketNotFound\n\t}\n\n\tif err := store.Delete(n.NatsKey(opt.Table, key)); err != nil {\n\t\treturn errors.Wrap(err, \"Failed to delete data\")\n\t}\n\n\treturn nil\n}\n\n// List returns any keys that match, or an empty list with no error if none matched.","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/nats-js-kv/nats.go#L254-L290","documentation":"Returned by Delete when opt.Table == \"DELETE_BUCKET\" and js.DeleteKeyValue(key) fails, i.e. the JetStream KV bucket stream could not be deleted. The in-memory bucket cache entry is removed first, so a failure here leaves the bucket server-side but forgotten locally until reconnect.","triggerScenarios":"Calling Delete(key, store.DeleteTable(\"DELETE_BUCKET\")) when the bucket does not exist server-side, the connection to NATS is broken, the account lacks permission to delete the stream, or the stream is in a state that forbids deletion (e.g. in-flight consumers/leadership election).","commonSituations":"Double-deleting the same bucket; trying to delete a bucket created by another account/subject permission; NATS server temporarily unavailable; deleting during cluster failover while the stream has no leader.","solutions":["Confirm the bucket actually exists with 'nats kv info <bucket>' before deleting, and treat not-found as success if idempotency is desired","Check the wrapped error: if it's a connection error, reinitialize the store and retry","Verify the NATS account has admin/publish permissions on the stream 'KV_<bucket>'","Retry after cluster settles if the error indicates no stream leader (transient quorum state)"],"exampleFix":"// before\nerr := st.Delete(bucketName, store.DeleteTable(\"DELETE_BUCKET\"))\n// after\nerr := st.Delete(bucketName, store.DeleteTable(\"DELETE_BUCKET\"))\nif err != nil && strings.Contains(err.Error(), \"not found\") {\n    err = nil // idempotent delete\n}","handlingStrategy":"try-catch","validationCode":"// check bucket exists before deleting\njs, _ := nc.JetStream()\nif _, err := js.StreamView(\"KV_\" + name); err != nil {\n    return nil // already gone; nothing to delete\n}","typeGuard":null,"tryCatchPattern":"if err := st.Delete(name, store.DeleteTable(\"DELETE_BUCKET\")); err != nil {\n    if strings.Contains(err.Error(), \"Failed to delete bucket\") {\n        // verify whether it still exists; treat already-gone as success\n        return nil\n    }\n    return err\n}","preventionTips":["Make bucket deletion idempotent in caller code","Ensure the account has permissions to delete streams","Avoid deleting buckets during cluster failover windows","Always verify with 'nats kv info' when a delete fails before retrying"],"tags":["nats","jetstream","delete","bucket"],"backgroundTag":"bucket-delete-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}