In SQL Server (T-SQL), you reseed or reset an IDENTITY column using DBCC CHECKIDENT ('schema.table_name', RESEED, new_reseed_value). The new_reseed_value parameter sets the last identity value assigned—meaning the next inserted row receives new_reseed_value + 1. If you delete rows and want the next insert to start at 1, pass 0 as the reseed value (DBCC CHECKIDENT ('dbo.table_name', RESEED, 0);).
1. Resetting Back to 1 After Deleting Rows
When you delete rows (for example, test records or an entire table via DELETE) and want the next inserted row to get id = 1, set the seed to 0:
-- Delete rows first (assuming no conflicting rows remain)
DELETE FROM dbo.orders
WHERE is_test_order = 1;
-- Next inserted row will receive order_id = 1
DBCC CHECKIDENT ('dbo.orders', RESEED, 0);
RESEED, 0sets the current identity value to0, so the nextINSERTgenerates1(assumingIDENTITY(1, 1)).- If you want the next row to get
500, pass499:
DBCC CHECKIDENT ('dbo.orders', RESEED, 499);
2. Reseeding to Match the Current Maximum ID (MAX(id))
If rows remain in the table and you deleted only a subset—or if the identity counter fell behind or got ahead of existing rows—resync it to the current maximum value so the next insert takes MAX(id) + 1:
DECLARE @max_id BIGINT = ISNULL((SELECT MAX(order_id) FROM dbo.orders), 0);
DBCC CHECKIDENT ('dbo.orders', RESEED, @max_id);
- If
dbo.ordershas rows up toorder_id = 42,_idis42, so the next inserted row gets43. - If
dbo.ordersis empty,_idis0, so the next inserted row gets1.
3. Checking the Current Identity Value Before Reseeding
Inspect the current identity value and seed/increment metadata without modifying anything using DBCC CHECKIDENT with NORESEED or IDENT_CURRENT():
-- Reports current identity value and current column value
DBCC CHECKIDENT ('dbo.orders', NORESEED);
-- Returns the last identity value generated for the table across any session/scope
SELECT IDENT_CURRENT('dbo.orders') AS current_identity_value;
DBCC CHECKIDENT ('dbo.orders', NORESEED)prints an informational message showing the current identity value and current column value without changing them.
4. DELETE + DBCC CHECKIDENT vs. TRUNCATE TABLE
TRUNCATE TABLE dbo.ordersdeallocates pages and automatically resets theIDENTITYcolumn back to its original seed (1), but it fails if referenced by a foreign key constraint and cannot be rolled back like a standard row-levelDELETE.DELETE+DBCC CHECKIDENTworks even when foreign keys exist (provided child rows are handled) and gives you exact control over the next generated number (RESEED, N).
5. Safe Execution Checklist in T-SQL
- Avoid primary key collisions: Never reseed lower than an existing
MAX(id)unless you have already deleted those rows—otherwise future inserts can fail with a primary key or unique constraint violation (Violation of PRIMARY KEY constraint). - Check foreign keys and external references: Reusing old IDs can confuse audit logs, historical reports, or foreign key relationships if IDs were previously issued and referenced elsewhere.
- Run during low write activity: Reseeding a table under heavy concurrent
INSERTtraffic can race with active inserts—coordinate resets during maintenance or inside a controlled batch. - Verify after reseeding: Run
DBCC CHECKIDENT ('dbo.table_name', NORESEED)or insert a test row in a rolled-back transaction (BEGIN TRAN…ROLLBACK TRAN) to confirm the next generated value.
