{"record":{"id":"9c5dd0b751ee8c4e","repo":"kopia/kopia","slug":"failed-to-rewrite-v-contents","errorCode":null,"errorMessage":"failed to rewrite %v contents","messagePattern":"failed to rewrite (.+?) contents","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"repo/maintenance/content_rewrite.go","lineNumber":163,"sourceCode":"\t\tToRewriteContentCount: uint64(toRewriteCount),\n\t\tToRewriteContentSize:  maintenancestats.ToUint64(toRewriteBytes),\n\t\tRewrittenContentCount: uint64(rewrittenCount),\n\t\tRewrittenContentSize:  maintenancestats.ToUint64(rewrittenBytes),\n\t\tRetainedContentCount:  uint64(retainedCount),\n\t\tRetainedContentSize:   maintenancestats.ToUint64(retainedBytes),\n\t}\n\n\tcontentlog.Log1(ctx, log, \"Rewritten contents\", result)\n\n\tif failedCount.Load() == 0 {\n\t\tif err := rep.ContentManager().Flush(ctx); err != nil {\n\t\t\treturn nil, errors.Wrap(err, \"error flushing repo\")\n\t\t}\n\n\t\treturn result, nil\n\t}\n\n\treturn nil, errors.Errorf(\"failed to rewrite %v contents\", failedCount.Load())\n}\n\nfunc getContentToRewrite(ctx context.Context, rep repo.DirectRepository, opt *RewriteContentsOptions) <-chan contentInfoOrError {\n\tch := make(chan contentInfoOrError)\n\n\tgo func() {\n\t\tdefer close(ch)\n\n\t\t// get content IDs listed on command line\n\t\tfindContentInfos(ctx, rep, ch, opt.ContentIDs)\n\n\t\t// add all content IDs from short packs\n\t\tif opt.ShortPacks {\n\t\t\tmp, mperr := rep.ContentReader().ContentFormat().GetMutableParameters(ctx)\n\t\t\tif mperr == nil {\n\t\t\t\tthreshold := int64(mp.MaxPackSize * shortPackThresholdPercent / 100) //nolint:mnd\n\t\t\t\tfindContentInShortPacks(ctx, rep, ch, threshold, opt)\n\t\t\t}","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/kopia/kopia/blob/82495e54b584c1ef6073c9e1be048f57f8aef078/repo/maintenance/content_rewrite.go#L145-L181","documentation":"RewriteContents tracks a counter (failedCount) of contents that could not be rewritten. If any failures occurred, it skips the flush and returns this error reporting how many contents failed to rewrite. It is a deliberate aggregate-failure signal rather than an unexpected exception.","triggerScenarios":"Any content queued by getContentToRewrite fails to rewrite — e.g. its underlying pack blob is missing/corrupted, content info lookup fails, or the rewrite worker encounters storage errors — so failedCount.Load() > 0 at the end of the run.","commonSituations":"Repository with corrupted or deleted pack files; partial content-info lookups failing for old contents; storage backend errors affecting a subset of blobs during a long-running full maintenance rewrite.","solutions":["Check logs emitted by the rewrite workers to identify which contents failed and why.","Re-run the rewrite command after fixing the underlying cause; contents that succeed are persisted incrementally.","If packs are missing or corrupted, restore them from replication/backup or drop unreferenced contents via repair tools.","If failures are confined to stale/deleted contents, investigate whether those contents should be excluded or the repo needs deeper repair (e.g. 'kopia repository repair')."],"exampleFix":"// before: abort entirely when any content fails\nreturn nil, errors.Errorf(\"failed to rewrite %v contents\", failedCount.Load())\n// after: log failures and allow forcing a flush of successful rewrites\nif failedCount.Load() > 0 {\n    log.Warnf(\"failed to rewrite %v contents\", failedCount.Load())\n    if opt.IgnoreFailures {\n        if err := rep.ContentManager().Flush(ctx); err != nil {\n            return nil, errors.Wrap(err, \"error flushing repo\")\n        }\n        return result, nil\n    }\n    return nil, errors.Errorf(\"failed to rewrite %v contents\", failedCount.Load())\n}","handlingStrategy":"try-catch","validationCode":"// pre-verify the contents that would be rewritten\nopts := &repo.VerifyContentsOptions{IncludeDeletedContents: true}\nif err := rep.VerifyContents(ctx, opts); err != nil {\n    return errors.Wrap(err, \"repository has unreadable contents; repair before rewrite\")\n}","typeGuard":null,"tryCatchPattern":"_, err := RewriteContents(ctx, rep, opt)\nif err != nil {\n    var nFailed int\n    if _, scanErr := fmt.Sscanf(err.Error(), \"failed to rewrite %d contents\", &nFailed); scanErr == nil && nFailed > 0 {\n        // some contents failed; inspect logs for which IDs failed, fix cause, re-run\n        log.Errorf(\"%d contents failed to rewrite; run verify/repair then retry\", nFailed)\n        return err\n    }\n    return err\n}","preventionTips":["Run content verification before enabling full maintenance rewrites.","Restore or repair missing/corrupted pack blobs before rewriting.","Monitor rewrite worker logs to catch which contents fail repeatedly.","Keep snapshots/replication so damaged packs can be restored."],"tags":["rewrite","maintenance","contents","corruption"],"backgroundTag":"operation-failed-partially","analyzedSha":"82495e54b584c1ef6073c9e1be048f57f8aef078","analyzedAt":"2026-09-07T20:35:21.689Z","contentChangedAt":"2026-09-07T20:35:21.689Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}