[GH-ISSUE #6649] [Feature]: Reuse freed inbound IDs on create #4441

Open
opened 2026-10-31 20:39:41 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @AlexeyLCP on GitHub (Sep 25, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6649

Problem

Deleting an inbound does not free its numeric id for the next create. The Inbounds table ends up with gaps (1, 2, 25, 26, 27). Delete 27, create a new inbound, and it becomes 28 — not 3.

Operators expect the next free id, not a counter that only ever grows.

Why it happens

inbounds.id is primaryKey; autoIncrement. AddInbound forces Id = 0, so SQLite AUTOINCREMENT / the Postgres sequence always assigns max+1. Deleted rowids are never reused. That is engine behavior, not a UI bug.

The only reset today is in DelInbound: when the table is empty, SQLite runs DELETE FROM sqlite_sequence WHERE name = 'inbounds'. Any remaining row leaves the counter where it is. UPDATE sqlite_sequence SET seq = MAX(id) would not fill gaps either, and that table does not exist on Postgres.

What should happen

On create, assign the smallest positive integer that is not currently used. Existing rows stay put.

Example: ids in use are 1, 2, 25, 26, 27. The next create gets 3, then 4, then 5. Do not renumber 25 → 3.

Do not renumber live inbounds

id is referenced by client_inbounds.inbound_id, hosts.inbound_id, client_traffics.inbound_id, sub-balancer inboundIds, node sync, and API clients. Rewriting live ids breaks those links. Client share links use UUID/password and tag, not this column — this request is only about the id assigned to a new inbound.

Safe reuse

Reuse an id only after delete has dropped every row that still points at it (hosts, client links, balancer membership, traffic). DelInbound already detaches clients, deletes hosts, and strips balancer ids. It does not delete client_traffics by inbound_id. A new inbound must not inherit leftover stats for that id.

Two concurrent creates must not grab the same gap (transaction + retry on unique conflict). Explicit id insert has to work on both SQLite and Postgres. Leave the empty-table sequence reset as it is.

Workarounds considered

  • Reset sqlite_sequence to MAX(id) — does not fill gaps, SQLite-only, races with leftovers.
  • Compact ids after every delete — rewrites every foreign key and anything external that cached the id.
  • A display-only index in the UI — the ID column and the API would still show 28.

Version

Current main. SQLite and Postgres. Inbounds page, ID column.

Originally created by @AlexeyLCP on GitHub (Sep 25, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6649 ### Problem Deleting an inbound does not free its numeric id for the next create. The Inbounds table ends up with gaps (`1, 2, 25, 26, 27`). Delete 27, create a new inbound, and it becomes 28 — not 3. Operators expect the next free id, not a counter that only ever grows. ### Why it happens `inbounds.id` is `primaryKey; autoIncrement`. `AddInbound` forces `Id = 0`, so SQLite `AUTOINCREMENT` / the Postgres sequence always assigns `max+1`. Deleted rowids are never reused. That is engine behavior, not a UI bug. The only reset today is in `DelInbound`: when the table is **empty**, SQLite runs `DELETE FROM sqlite_sequence WHERE name = 'inbounds'`. Any remaining row leaves the counter where it is. `UPDATE sqlite_sequence SET seq = MAX(id)` would not fill gaps either, and that table does not exist on Postgres. ### What should happen On create, assign the smallest positive integer that is not currently used. Existing rows stay put. Example: ids in use are `1, 2, 25, 26, 27`. The next create gets **3**, then 4, then 5. Do not renumber 25 → 3. ### Do not renumber live inbounds `id` is referenced by `client_inbounds.inbound_id`, `hosts.inbound_id`, `client_traffics.inbound_id`, sub-balancer `inboundIds`, node sync, and API clients. Rewriting live ids breaks those links. Client share links use UUID/password and tag, not this column — this request is only about the id assigned to a **new** inbound. ### Safe reuse Reuse an id only after delete has dropped every row that still points at it (hosts, client links, balancer membership, traffic). `DelInbound` already detaches clients, deletes hosts, and strips balancer ids. It does **not** delete `client_traffics` by `inbound_id`. A new inbound must not inherit leftover stats for that id. Two concurrent creates must not grab the same gap (transaction + retry on unique conflict). Explicit id insert has to work on both SQLite and Postgres. Leave the empty-table sequence reset as it is. ### Workarounds considered - Reset `sqlite_sequence` to `MAX(id)` — does not fill gaps, SQLite-only, races with leftovers. - Compact ids after every delete — rewrites every foreign key and anything external that cached the id. - A display-only index in the UI — the ID column and the API would still show 28. ### Version Current `main`. SQLite and Postgres. Inbounds page, ID column.
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#4441
No description provided.