{"record":{"id":"67050fa8de48730a","repo":"crowdsecurity/crowdsec","slug":"unable-to-delete-allowlist-contents-w","errorCode":null,"errorMessage":"unable to delete allowlist contents: %w","messagePattern":"unable to delete allowlist contents: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/database/allowlists.go","lineNumber":224,"sourceCode":"\nfunc (c *Client) UpdateAllowlistMeta(ctx context.Context, allowlistID string, name string, description string) error {\n\tc.Log.Debugf(\"updating allowlist %s meta\", name)\n\n\terr := c.Ent.AllowList.Update().Where(allowlist.AllowlistIDEQ(allowlistID)).SetName(name).SetDescription(description).Exec(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"unable to update allowlist: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (c *Client) ReplaceAllowlist(ctx context.Context, list *ent.AllowList, items []*models.AllowlistItem, fromConsole bool) (int, error) {\n\tc.Log.Debugf(\"replacing values in allowlist %s\", list.Name)\n\tc.Log.Tracef(\"items: %+v\", items)\n\n\t_, err := c.Ent.AllowListItem.Delete().Where(allowlistitem.HasAllowlistWith(allowlist.IDEQ(list.ID))).Exec(ctx)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"unable to delete allowlist contents: %w\", err)\n\t}\n\n\tadded, err := c.AddToAllowlist(ctx, list, items)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"unable to add values to allowlist: %w\", err)\n\t}\n\n\tif !list.FromConsole && fromConsole {\n\t\tc.Log.Infof(\"marking allowlist %s as managed from console and replacing its content\", list.Name)\n\n\t\terr = c.Ent.AllowList.Update().SetFromConsole(fromConsole).Where(allowlist.IDEQ(list.ID)).Exec(ctx)\n\t\tif err != nil {\n\t\t\treturn 0, fmt.Errorf(\"unable to update allowlist: %w\", err)\n\t\t}\n\t}\n\n\treturn added, nil\n}","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/allowlists.go#L206-L242","documentation":"ReplaceAllowlist first deletes ALL existing items of the target allowlist, then re-adds the provided set. If the initial bulk delete of the contents fails, it returns 'unable to delete allowlist contents: %w'. Because the delete runs outside a transaction together with the re-add, a failure here can leave the allowlist emptied if AddToAllowlist subsequently fails.","triggerScenarios":"Client.ReplaceAllowlist (called from updateOneAllowlist during console sync) when the DELETE on allowlist_items fails: DB unreachable, locked, corrupted table, or cancelled context.","commonSituations":"Console allowlist sync against a database that went down or is locked (SQLite concurrent writers); disk full preventing the delete; interrupted network to a remote MySQL/Postgres.","solutions":["Inspect the wrapped driver error and fix the DB-level cause (connectivity, locks, disk space)","After a failure, verify allowlist contents — the delete may have partially applied; re-run ReplaceAllowlist to reach a consistent state","Reduce concurrent DB writers or move from SQLite to MySQL/Postgres to avoid lock contention","Retry the replace once the database is healthy; the operation is idempotent since it deletes before inserting"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if _, err := client.GetAllowListByID(ctx, list.AllowlistID, false); err != nil {\n    return fmt.Errorf(\"target allowlist unavailable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"added, err := client.ReplaceAllowlist(ctx, list, items, fromConsole)\nif err != nil && strings.Contains(err.Error(), \"unable to delete allowlist contents\") {\n    // DB-level delete failure; contents may be partially deleted — re-run ReplaceAllowlist after fixing the DB\n}","preventionTips":["Re-run a failed replace: the operation is idempotent (delete-then-insert)","Verify allowlist contents after any failure — the delete step is not transactional with the re-add","Ensure DB health (disk, locks, connectivity) before console syncs"],"tags":["database","allowlist","delete","replace","sql"],"backgroundTag":"database-write-failed","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}