{"record":{"id":"4144a1e0e1c8054f","repo":"henrygd/beszel","slug":"failed-to-delete-from-s-v","errorCode":null,"errorMessage":"failed to delete from %s: %v","messagePattern":"failed to delete from (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/records/records_deletion.go","lineNumber":96,"sourceCode":"\tnow := time.Now().UTC()\n\n\tfor _, collection := range collections {\n\t\t// Build the WHERE clause\n\t\tvar conditionParts []string\n\t\tvar params dbx.Params = make(map[string]any)\n\t\tfor i := range recordData {\n\t\t\trd := recordData[i]\n\t\t\t// Create parameterized condition for this record type\n\t\t\tdateParam := fmt.Sprintf(\"date%d\", i)\n\t\t\tconditionParts = append(conditionParts, fmt.Sprintf(\"(type = '%s' AND created < {:%s})\", rd.recordType, dateParam))\n\t\t\tparams[dateParam] = now.Add(-rd.retention)\n\t\t}\n\t\t// Combine conditions with OR\n\t\tconditionStr := strings.Join(conditionParts, \" OR \")\n\t\t// Construct and execute the full raw query\n\t\trawQuery := fmt.Sprintf(\"DELETE FROM %s WHERE %s\", collection, conditionStr)\n\t\tif _, err := app.DB().NewQuery(rawQuery).Bind(params).Execute(); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to delete from %s: %v\", collection, err)\n\t\t}\n\t}\n\treturn nil\n}\n\n// Deletes systemd service records that haven't been updated in the last 20 minutes\nfunc deleteOldSystemdServiceRecords(app core.App) error {\n\tnow := time.Now().UTC()\n\ttwentyMinutesAgo := now.Add(-20 * time.Minute)\n\n\t// Delete systemd service records where updated < twentyMinutesAgo\n\t_, err := app.DB().NewQuery(\"DELETE FROM systemd_services WHERE updated < {:updated}\").Bind(dbx.Params{\"updated\": twentyMinutesAgo.UnixMilli()}).Execute()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to delete old systemd service records: %v\", err)\n\t}\n\n\treturn nil\n}","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/henrygd/beszel/blob/b38fb7dafa60812cc22e6a84ce313e94f1ce0a32/internal/records/records_deletion.go#L78-L114","documentation":"deleteOldSystemStats builds a raw DELETE query per stats collection with OR-joined time conditions and executes it against the PocketBase DB. If execution fails, the collection name is embedded in this error so the failing table is identified.","triggerScenarios":"DB-level failure executing the raw DELETE on a system_stats collection: locked database, disk full, permission issues, or a collection name that no longer exists in the schema.","commonSituations":"SQLite file locked by another process/backup job; disk quota exceeded on the host; schema migrations renaming/dropping a stats collection while retention cleanup still references it.","solutions":["Check the wrapped error (%v) for the SQLite/dxdb root cause (locked, disk full, no such table)","Verify the stats collections still exist with the expected names in the PocketBase schema","Check disk space and that no backup/other process holds an exclusive lock on the DB during cleanup"],"exampleFix":"// before\nrawQuery := fmt.Sprintf(\"DELETE FROM %s WHERE %s\", collection, conditionStr)\n// after\n// ensure collection exists and log the underlying error\nif _, err := app.DB().NewQuery(rawQuery).Bind(params).Execute(); err != nil {\n    return fmt.Errorf(\"failed to delete from %s: %w\", collection, err)\n}","handlingStrategy":"try-catch","validationCode":"if !collectionExists(app, collection) {\n    return fmt.Errorf(\"stats collection %s missing from schema\", collection)\n}\nif diskAlmostFull() { return errors.New(\"insufficient disk space for retention cleanup\") }","typeGuard":null,"tryCatchPattern":"if err := records.DeleteOldSystemStats(app); err != nil {\n    log.Error().Err(err).Msg(\"system stats retention cleanup failed\")\n    // alert/check DB lock and disk space; retry next cycle\n}","preventionTips":["Monitor disk space on the hub host","Avoid exclusive DB locks (backups) during cleanup windows","Verify stats collection names after schema migrations","Alert on retention-cleanup failures"],"tags":["database","sqlite","retention","delete"],"backgroundTag":"database-delete-failed","analyzedSha":"b38fb7dafa60812cc22e6a84ce313e94f1ce0a32","analyzedAt":"2026-08-31T15:10:10.149Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}