[PR #5842] [CLOSED] fix: clean up node_client_traffics when deleting a client #6007

Closed
opened 2026-11-02 04:05:43 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5842
Author: @guocn
Created: 7/7/2026
Status: ❌ Closed

Base: main ← Head: fix/delete-node-client-traffics


📝 Commits (1)

  • 456698a fix: clean up node_client_traffics when deleting a client

📊 Changes

1 file changed (+6 additions, -0 deletions)

View changed files

📝 internal/web/service/client_crud.go (+6 -0)

📄 Description

Summary

When a client is deleted, the node_client_traffics table is not cleaned up. This PR adds the missing Delete call in both Delete() and DeleteByEmail() code paths.

Problem

Deleting a client via the panel UI or API cleans up all related tables — client_traffics, client_global_traffic, inbound_client_ips, client_inbounds, client_external_links, the inbound settings JSON, and the ClientRecord row — but node_client_traffics is never touched.

This means:

  • Orphaned rows accumulate in node_client_traffics for every client ever created and deleted, across every registered node.
  • Manual cleanup is not permanent: even if you DELETE FROM node_client_traffics WHERE email = ? directly, the next node traffic sync cycle re-creates the rows — the master re-inserts orphaned traffic based on stale xray stats counters from remote nodes.
  • The only way to permanently remove them today is to stop x-ui, delete the rows, then restart — not practical in production.

Fix

Add db.Where("email = ?", email).Delete(&model.NodeClientTraffic{}) in both deletion paths, alongside the existing client_traffics / inbound_client_ips cleanup.

This ensures consistency: when keepTraffic=false, all traffic-related tables for that email are purged, including per-node records.

Changes

// In Delete() — inside the transaction:
+ if err := tx.Where("email = ?", existing.Email).Delete(&model.NodeClientTraffic{}).Error; err != nil {
+     return err
+ }

// In DeleteByEmail() — fallback path when no ClientRecord exists:
+ if err := db.Where("email = ?", email).Delete(&model.NodeClientTraffic{}).Error; err != nil {
+     return needRestart, err
+ }

Testing

  • Verified on a production multi-node deployment (1 master + 15 remote nodes, SQLite): after applying this fix, deleting a client via the panel UI now removes all rows from node_client_traffics immediately and permanently.
  • The keepTraffic=true path (detach) correctly preserves node_client_traffics rows, same as it preserves client_traffics.

Fixes #5841


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/5842 **Author:** [@guocn](https://github.com/guocn) **Created:** 7/7/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/delete-node-client-traffics` --- ### 📝 Commits (1) - [`456698a`](https://github.com/MHSanaei/3x-ui/commit/456698aeaf2e7ae32010db0ba93c112caca3de3a) fix: clean up node_client_traffics when deleting a client ### 📊 Changes **1 file changed** (+6 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/client_crud.go` (+6 -0) </details> ### 📄 Description ## Summary When a client is deleted, the `node_client_traffics` table is not cleaned up. This PR adds the missing `Delete` call in both `Delete()` and `DeleteByEmail()` code paths. ## Problem Deleting a client via the panel UI or API cleans up all related tables — `client_traffics`, `client_global_traffic`, `inbound_client_ips`, `client_inbounds`, `client_external_links`, the inbound settings JSON, and the `ClientRecord` row — but `node_client_traffics` is never touched. This means: - **Orphaned rows accumulate** in `node_client_traffics` for every client ever created and deleted, across every registered node. - **Manual cleanup is not permanent**: even if you `DELETE FROM node_client_traffics WHERE email = ?` directly, the next node traffic sync cycle **re-creates** the rows — the master re-inserts orphaned traffic based on stale xray stats counters from remote nodes. - The only way to permanently remove them today is to stop x-ui, delete the rows, then restart — not practical in production. ## Fix Add `db.Where("email = ?", email).Delete(&model.NodeClientTraffic{})` in both deletion paths, alongside the existing `client_traffics` / `inbound_client_ips` cleanup. This ensures consistency: when `keepTraffic=false`, **all** traffic-related tables for that email are purged, including per-node records. ## Changes ```diff // In Delete() — inside the transaction: + if err := tx.Where("email = ?", existing.Email).Delete(&model.NodeClientTraffic{}).Error; err != nil { + return err + } // In DeleteByEmail() — fallback path when no ClientRecord exists: + if err := db.Where("email = ?", email).Delete(&model.NodeClientTraffic{}).Error; err != nil { + return needRestart, err + } ``` ## Testing - Verified on a production multi-node deployment (1 master + 15 remote nodes, SQLite): after applying this fix, deleting a client via the panel UI now removes all rows from `node_client_traffics` immediately and permanently. - The `keepTraffic=true` path (detach) correctly preserves `node_client_traffics` rows, same as it preserves `client_traffics`. Fixes #5841 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:05:43 +07:00
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#6007
No description provided.