[GH-ISSUE #5841] Bug: Deleting a client does not clean up table (orphaned rows persist and get re-created) #4054

Closed
opened 2026-10-31 20:36:49 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @guocn on GitHub (Jul 7, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5841

Bug Description

When a client is deleted via the panel (UI or API), the node_client_traffics table is not cleaned up. All other related tables — client_traffics, client_global_traffic, inbound_client_ips, client_inbounds, client_external_links, and the inbound settings JSON — are properly cleaned. However, node_client_traffics is never touched, leaving orphaned traffic rows for the deleted client across every registered node.

Steps to Reproduce

  1. Set up a master panel with one or more remote nodes (multi-node topology)
  2. Add a client (e.g. email: testuser) and attach it to an inbound
  3. Let the panel run for a while so traffic data accumulates
  4. Delete the client via the panel UI or POST /panel/api/clients/del/:email
  5. Query the database:
SELECT DISTINCT email FROM node_client_traffics WHERE email = 'testuser';
-- Returns rows: the deleted client still exists in node_client_traffics

Expected Behavior

Deleting a client should remove all associated rows from node_client_traffics (same as it already does for client_traffics, client_global_traffic, etc.).

Actual Behavior

node_client_traffics rows for the deleted email remain indefinitely. Worse, even if you manually DELETE them while x-ui is running, the next node traffic sync cycle re-creates them — the master node re-inserts orphaned traffic rows based on stale xray stats counters from remote nodes.

Root Cause (from source analysis)

In internal/web/service/client_crud.go, the Delete() method:

if !keepTraffic && existing.Email != "" {
    db.Where("email = ?", existing.Email).Delete(&xray.ClientTraffic{})     // client_traffics ✅
    clearGlobalTraffic(db, existing.Email)                                    // client_global_traffic ✅
    db.Where("client_email = ?", existing.Email).Delete(&model.InboundClientIps{})  // inbound_client_ips ✅
}
// ← node_client_traffics: NOT cleaned ❌

The same omission exists in DeleteByEmail().

Impact

  • Orphaned users appear in any third-party traffic monitoring that reads node_client_traffics
  • These ghost entries cannot be removed permanently without stopping x-ui first (the sync cycle re-creates them while the panel is running)
  • Over time, the table accumulates rows for every client ever created and deleted

Environment

  • 3x-ui version: v3.4.0
  • Xray version: v26.6.22
  • Database: SQLite
  • Topology: Multi-node (1 master + multiple remote nodes)

Suggested Fix

Add cleanup of node_client_traffics in both Delete() and DeleteByEmail():

if !keepTraffic && existing.Email != "" {
    // existing cleanup...
    db.Where("email = ?", existing.Email).Delete(&xray.ClientTraffic{})
    clearGlobalTraffic(db, existing.Email)
    db.Where("client_email = ?", existing.Email).Delete(&model.InboundClientIps{})
    // Add this line:
    db.Where("email = ?", existing.Email).Delete(&model.NodeClientTraffic{})
}
Originally created by @guocn on GitHub (Jul 7, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5841 ## Bug Description When a client is deleted via the panel (UI or API), the `node_client_traffics` table is **not cleaned up**. All other related tables — `client_traffics`, `client_global_traffic`, `inbound_client_ips`, `client_inbounds`, `client_external_links`, and the inbound settings JSON — are properly cleaned. However, `node_client_traffics` is never touched, leaving orphaned traffic rows for the deleted client across every registered node. ## Steps to Reproduce 1. Set up a master panel with one or more remote nodes (multi-node topology) 2. Add a client (e.g. email: `testuser`) and attach it to an inbound 3. Let the panel run for a while so traffic data accumulates 4. Delete the client via the panel UI or `POST /panel/api/clients/del/:email` 5. Query the database: ```sql SELECT DISTINCT email FROM node_client_traffics WHERE email = 'testuser'; -- Returns rows: the deleted client still exists in node_client_traffics ``` ## Expected Behavior Deleting a client should remove all associated rows from `node_client_traffics` (same as it already does for `client_traffics`, `client_global_traffic`, etc.). ## Actual Behavior `node_client_traffics` rows for the deleted email remain indefinitely. Worse, even if you manually `DELETE` them while x-ui is running, the next node traffic sync cycle **re-creates** them — the master node re-inserts orphaned traffic rows based on stale xray stats counters from remote nodes. ## Root Cause (from source analysis) In `internal/web/service/client_crud.go`, the `Delete()` method: ```go if !keepTraffic && existing.Email != "" { db.Where("email = ?", existing.Email).Delete(&xray.ClientTraffic{}) // client_traffics ✅ clearGlobalTraffic(db, existing.Email) // client_global_traffic ✅ db.Where("client_email = ?", existing.Email).Delete(&model.InboundClientIps{}) // inbound_client_ips ✅ } // ← node_client_traffics: NOT cleaned ❌ ``` The same omission exists in `DeleteByEmail()`. ## Impact - Orphaned users appear in any third-party traffic monitoring that reads `node_client_traffics` - These ghost entries cannot be removed permanently without stopping x-ui first (the sync cycle re-creates them while the panel is running) - Over time, the table accumulates rows for every client ever created and deleted ## Environment - 3x-ui version: v3.4.0 - Xray version: v26.6.22 - Database: SQLite - Topology: Multi-node (1 master + multiple remote nodes) ## Suggested Fix Add cleanup of `node_client_traffics` in both `Delete()` and `DeleteByEmail()`: ```go if !keepTraffic && existing.Email != "" { // existing cleanup... db.Where("email = ?", existing.Email).Delete(&xray.ClientTraffic{}) clearGlobalTraffic(db, existing.Email) db.Where("client_email = ?", existing.Email).Delete(&model.InboundClientIps{}) // Add this line: db.Where("email = ?", existing.Email).Delete(&model.NodeClientTraffic{}) } ```
bsakaguchi 2026-10-31 20:36:49 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Jul 7, 2026):

Confirmed. I checked internal/web/service/client_crud.go against the current main (past v3.4.2/dev-latest, so this is not yet fixed):

  • Delete() (lines 452-523): the !keepTraffic block (507-517) deletes xray.ClientTraffic, calls clearGlobalTraffic, and deletes model.InboundClientIps - but never touches model.NodeClientTraffic.
  • DeleteByEmail() (lines 618-662): the equivalent block (649-660) has the same omission.

This is also inconsistent with the rest of the codebase: InboundService.DelClientStat and delClientStatsByEmails (internal/web/service/inbound_traffic.go:477-509) are the established helpers for removing a client's traffic records, and both of them do delete the matching model.NodeClientTraffic rows. They're used from the raw-settings-JSON client removal path (inbound.go:1454) and the bulk-operations paths (client_bulk.go:1023, client_inbound_apply.go), but client_crud.go's Delete()/DeleteByEmail() - which back POST /panel/api/clients/del/:email (internal/web/controller/client.go:179-190) - duplicate part of that cleanup inline instead of calling the shared helper, and drop the node_client_traffics line in the process.

On the "re-creation" part: node_client_traffics rows are only swept during node traffic sync (internal/web/service/inbound_node.go, setRemoteTrafficLocked) once a node's own snapshot stops reporting stats for that email. There is a 90-second delete tombstone (deleteTombstoneTTL in internal/web/service/client_locks.go) that blocks resurrection immediately after a delete, but nothing beyond that window - so if a node keeps reporting a stats counter for the removed email, the next sync re-upserts the baseline row, matching what you're seeing.

Your suggested fix (adding the model.NodeClientTraffic delete alongside the existing client_traffics/global-traffic/inbound_client_ips cleanup in both Delete() and DeleteByEmail()) matches the pattern already used by DelClientStat elsewhere in the codebase.

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:4898997447 --> @github-actions[bot] commented on GitHub (Jul 7, 2026): Confirmed. I checked `internal/web/service/client_crud.go` against the current `main` (past `v3.4.2`/`dev-latest`, so this is not yet fixed): - `Delete()` (lines 452-523): the `!keepTraffic` block (507-517) deletes `xray.ClientTraffic`, calls `clearGlobalTraffic`, and deletes `model.InboundClientIps` - but never touches `model.NodeClientTraffic`. - `DeleteByEmail()` (lines 618-662): the equivalent block (649-660) has the same omission. This is also inconsistent with the rest of the codebase: `InboundService.DelClientStat` and `delClientStatsByEmails` (`internal/web/service/inbound_traffic.go:477-509`) are the established helpers for removing a client's traffic records, and both of them *do* delete the matching `model.NodeClientTraffic` rows. They're used from the raw-settings-JSON client removal path (`inbound.go:1454`) and the bulk-operations paths (`client_bulk.go:1023`, `client_inbound_apply.go`), but `client_crud.go`'s `Delete()`/`DeleteByEmail()` - which back `POST /panel/api/clients/del/:email` (`internal/web/controller/client.go:179-190`) - duplicate part of that cleanup inline instead of calling the shared helper, and drop the `node_client_traffics` line in the process. On the "re-creation" part: `node_client_traffics` rows are only swept during node traffic sync (`internal/web/service/inbound_node.go`, `setRemoteTrafficLocked`) once a node's own snapshot stops reporting stats for that email. There is a 90-second delete tombstone (`deleteTombstoneTTL` in `internal/web/service/client_locks.go`) that blocks resurrection immediately after a delete, but nothing beyond that window - so if a node keeps reporting a stats counter for the removed email, the next sync re-upserts the baseline row, matching what you're seeing. Your suggested fix (adding the `model.NodeClientTraffic` delete alongside the existing `client_traffics`/global-traffic/`inbound_client_ips` cleanup in both `Delete()` and `DeleteByEmail()`) matches the pattern already used by `DelClientStat` elsewhere in the codebase. *This reply was generated automatically; a maintainer may follow up.*
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#4054
No description provided.