How do you reseed and reset an IDENTITY column after deleting rows in SQL Server? | A1Lab@HumairaM to SQLEnglish • 2 days ago

How do you reseed and reset an IDENTITY column after deleting rows in SQL Server?

message-square
0

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, 0 sets the current identity value to 0, so the next INSERT generates 1 (assuming IDENTITY(1, 1)).
  • If you want the next row to get 500, pass 499:
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.orders has rows up to order_id = 42, @max_id is 42, so the next inserted row gets 43.
  • If dbo.orders is empty, @max_id is 0, so the next inserted row gets 1.

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.orders deallocates pages and automatically resets the IDENTITY column 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-level DELETE.
  • DELETE + DBCC CHECKIDENT works 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

  1. 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).
  2. 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.
  3. Run during low write activity: Reseeding a table under heavy concurrent INSERT traffic can race with active inserts—coordinate resets during maintenance or inside a controlled batch.
  4. 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.