MCPcopy
hub / github.com/nats-io/nats.go / Delete

Method Delete

jetstream/kv.go:1125–1172  ·  view source on GitHub ↗

Delete will place a delete marker and leave all revisions.

(ctx context.Context, key string, opts ...KVDeleteOpt)

Source from the content-addressed store, hash-verified

1123
1124// Delete will place a delete marker and leave all revisions.
1125func (kv *kvs) Delete(ctx context.Context, key string, opts ...KVDeleteOpt) error {
1126 if !keyValid(key) {
1127 return ErrInvalidKey
1128 }
1129
1130 var b strings.Builder
1131 if kv.useJSPfx {
1132 b.WriteString(kv.js.opts.apiPrefix)
1133 }
1134 if kv.putPre != "" {
1135 b.WriteString(kv.putPre)
1136 } else {
1137 b.WriteString(kv.pre)
1138 }
1139 b.WriteString(key)
1140
1141 // DEL op marker. For watch functionality.
1142 m := nats.NewMsg(b.String())
1143
1144 var o deleteOpts
1145 for _, opt := range opts {
1146 if opt != nil {
1147 if err := opt.configureDelete(&o); err != nil {
1148 return err
1149 }
1150 }
1151 }
1152
1153 if o.purge {
1154 m.Header.Set(kvop, kvpurge)
1155 m.Header.Set(MsgRollup, MsgRollupSubject)
1156 } else {
1157 m.Header.Set(kvop, kvdel)
1158 }
1159 pubOpts := make([]PublishOpt, 0)
1160 if o.ttl > 0 && o.purge {
1161 pubOpts = append(pubOpts, WithMsgTTL(o.ttl))
1162 } else if o.ttl > 0 {
1163 return ErrTTLOnDeleteNotSupported
1164 }
1165
1166 if o.revision != 0 {
1167 m.Header.Set(ExpectedLastSubjSeqHeader, strconv.FormatUint(o.revision, 10))
1168 }
1169
1170 _, err := kv.js.PublishMsg(ctx, m, pubOpts...)
1171 return err
1172}
1173
1174// Purge will place a delete marker and remove all previous revisions.
1175func (kv *kvs) Purge(ctx context.Context, key string, opts ...KVDeleteOpt) error {

Callers 1

PurgeMethod · 0.95

Calls 6

WithMsgTTLFunction · 0.85
keyValidFunction · 0.70
configureDeleteMethod · 0.65
SetMethod · 0.65
PublishMsgMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected