Quick Answer
Find who deleted records in SQL Server in 4 ways. Query fn_dblog on the online log for the login and the time. Query a log backup with fn_dump_dblog when the online log is cleared. Enable SQL Server Audit to catch the next delete. Open the LDF in a log reader for a ready user list. Act before the log is truncated.
Finding who deleted records in SQL Server starts in the transaction log. Every delete writes a log record with a transaction ID that leads to the login behind it.
This guide shows what the log records, how long the records last and 4 methods that tell you who deleted records in SQL Server with the login, the time and the table.
How does SQL Server Record Who Deleted a Row?
SQL Server writes a LOP_DELETE_ROWS record for every deleted row and a LOP_BEGIN_XACT record when the transaction starts. Begin records store the login SID and the start time. Join both records on the transaction ID and you get the user behind the delete.
| Log column | Record type | What it tells you |
|---|---|---|
| Operation | All records | LOP_DELETE_ROWS marks a deleted row and LOP_BEGIN_XACT marks the start |
| Transaction ID | All records | Links the delete to its begin record |
| Transaction SID | LOP_BEGIN_XACT | Login of the user who started the transaction |
| Begin Time | LOP_BEGIN_XACT | Start time of the transaction |
| AllocUnitName | LOP_DELETE_ROWS | Table and index that lost the row |
Your tables store no delete history. Only the log, an audit or a trigger records the user.
How Long do Deleted Rows Last in the Log?
Log records last until SQL Server truncates that part of the log. Your recovery model decides when that happens.
| Recovery model | Log space is reused after | Your chance to find the user |
|---|---|---|
| FULL | A log backup completes | Until the next log backup. After that open the log backup file |
| BULK_LOGGED | A log backup completes | Same as FULL for deletes |
| SIMPLE | A checkpoint completes | Short. Act within minutes on a busy database |
Heads Up. Pause your log backup schedule and copy the LDF and the latest log backups before you start. New log backups and checkpoints clear the records you need.
Our recovery models and backup types guide explains these three models.
What are the System and Software Specifications?
These tables list what you need before you start. Log reader figures come from the product page.
System Specifications
| Component | Requirement |
|---|---|
| Operating system | Windows versions up to Windows 10 for the log reader software |
| Memory | 512 MB of RAM for the log reader software |
| Storage | 50 MB of free disk space for the software and extra space for a copy of the LDF file |
| Server access | sysadmin role on the SQL Server instance for fn_dblog and fn_dump_dblog |
Software Specifications
| Item | Requirement |
|---|---|
| SQL Server version | fn_dblog works on SQL Server 2005 and later. The log reader software covers SQL Server 2017 and earlier |
| Database files | The LDF file with its healthy MDF file. The log reader cannot show transactions when the MDF is corrupt |
| Backup files | Log backup files (.trn) for fn_dump_dblog |
| Client tool | SQL Server Management Studio or sqlcmd |
Which 4 Methods Show Who Deleted Records in SQL Server?
| Method | Best for | Main limit |
|---|---|---|
| Query fn_dblog | A delete that is still in the online log | Undocumented and returns 129 columns |
| Query fn_dump_dblog on a log backup | A delete that moved into a log backup | The begin record must sit in the same file |
| Enable SQL Server Audit | The next delete on a table | Records nothing about past deletes |
| Open the LDF in a log reader | A ready list of users, times and queries | Paid and limited to SQL Server 2017 and earlier |
Method #1: Query the Online Log With fn_dblog (Free, Active Log Only)
Microsoft leaves fn_dblog undocumented. fn_dblog returns the active part of the transaction log of your current database. Join the delete records with their begin records to name the login.
USE YourDatabase;
GO
SELECT b.[Begin Time],
SUSER_SNAME(b.[Transaction SID]) AS login_name,
d.[Transaction ID],
d.[AllocUnitName],
COUNT(*) AS log_records
FROM sys.fn_dblog(NULL, NULL) AS d
JOIN sys.fn_dblog(NULL, NULL) AS b
ON b.[Transaction ID] = d.[Transaction ID]
AND b.[Operation] = N'LOP_BEGIN_XACT'
WHERE d.[Operation] = N'LOP_DELETE_ROWS'
AND d.[AllocUnitName] LIKE N'dbo.Orders%'
GROUP BY b.[Begin Time], b.[Transaction SID], d.[Transaction ID], d.[AllocUnitName]
ORDER BY b.[Begin Time] DESC;
- Replace the table name. Swap dbo.Orders for your table.
- Check the login. SUSER_SNAME returns NULL when the login no longer exists. Use the SID in that case.
- Count the records. Every deleted row can add records for its indexes.
Heads Up. Microsoft gives no support for fn_dblog. Execute the T-SQL on this page on a restored copy first because it is untested on your build.
See our reading SQL Server transaction logs guide for the columns of fn_dblog.
Method #2: Query a Log Backup With fn_dump_dblog (Free, After Log Truncation)
Your online log loses old records after a log backup. Those records still exist inside the log backup file. fn_dump_dblog opens that file and returns the same columns as fn_dblog.
SELECT d.[Current LSN], d.[Operation], d.[Transaction ID], d.[AllocUnitName]
FROM fn_dump_dblog(NULL, NULL, N'DISK', 1, N'D:\Backups\YourDatabase_log_0900.trn',
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT) AS d
WHERE d.[Operation] = N'LOP_DELETE_ROWS';
Microsoft gives fn_dump_dblog 68 parameters. Sixty three DEFAULT values fill the unused file slots. Add the begin record by selecting LOP_BEGIN_XACT from the same call and join on the transaction ID like Method #1.
Heads Up. Transactions that started in an earlier log backup have their begin record in that earlier file. Query the earlier backup too when the login column is empty.
Our fn_dblog to find a transaction in backup guide shows the restore based version of this method.
Method #3: Enable SQL Server Audit for the Next Delete (Free, Forward Only)
Audit cannot show a delete that already happened. Audit records every later delete on the table with the login, the time and the statement.
USE master;
GO
CREATE SERVER AUDIT [DeleteAudit]
TO FILE (FILEPATH = N'D:\Audit\');
ALTER SERVER AUDIT [DeleteAudit] WITH (STATE = ON);
GO
USE YourDatabase;
GO
CREATE DATABASE AUDIT SPECIFICATION [DeleteSpec]
FOR SERVER AUDIT [DeleteAudit]
ADD (DELETE ON OBJECT::dbo.Orders BY public)
WITH (STATE = ON);
GO
SELECT event_time, server_principal_name, statement
FROM sys.fn_get_audit_file(N'D:\Audit\*.sqlaudit', DEFAULT, DEFAULT)
WHERE action_id = 'DL';
- Folder. Create the D:\Audit folder first and grant the SQL Server service account modify access.
- Scope. Your specification above watches one table. Add more tables with more ADD lines.
- Edition. Check your edition for database audit specification support before you plan on it.
Our tracking DML operations guide compares audit with triggers.
Method #4: Open the LDF File With SQL Server Log Explorer (Paid, Ready List of Users)
Queries on fn_dblog return raw log records. SQL Server Log Explorer analyzes Insert, Update and Delete transactions from a live server or from an offline LDF file. Its preview shows the transaction name, time, table, login name and query, so you see who deleted records in SQL Server without writing joins.
- Select an online server or an offline LDF file of your database.
- Scan the log and open the Delete transactions in the preview.
- Note the login name, time, table and query of every delete.
- Filter the result by transaction type or by date range.
- Export the result as a SQL Server database, SQL scripts or CSV.
Heads Up. SQL Server Log Explorer covers SQL Server 2017 and earlier and needs a healthy MDF file. According to its FAQ, recovery of deleted records works for databases in the SIMPLE recovery model.
Export to a SQL script when you also want to put the deleted rows back. Our recover deleted records in SQL Server guide covers that step.
Which Steps Follow Once You Find the User?
Four steps protect the data and the evidence.
- Save the result. Export the rows or the log records to a dated file.
- Recover the rows. Restore a backup to a test database and copy the missing rows back.
- Close the gap. Remove delete rights from logins that do not need them.
- Audit the table. Apply Method #3 so the next delete names its user at once.
Our how to rollback a transaction guide covers the case where the delete transaction is still open.
FAQs
Can You Find Who Deleted Records in SQL Server After the Fact?
Yes. Query the transaction log while it still contains the delete or query the log backup that contains it. After the log is gone only a backup based restore remains.
Does SQL Server Store a Delete History by Default?
No. SQL Server stores no delete history in your tables. Delete records live in the transaction log until truncation.
Which Log Column Shows the User Who Deleted Rows?
Transaction SID on the LOP_BEGIN_XACT record. Pass it to SUSER_SNAME to get the login name.
Does the fn_dblog Function Work in the SIMPLE Recovery Model?
Yes. fn_dblog shows the active log only. Checkpoints clear old records quickly so query right after the delete.
Can You Find the Deleted Values With the fn_dblog Function?
Partly. Row bytes sit in the log columns in a binary form. Log readers convert them to normal columns for you.
Does Dropping a Table Appear as LOP_DELETE_ROWS?
No. Drops log different records. Look for the transaction name DROPOBJ in fn_dblog or open the default trace.
How do You Stop Users From Deleting Rows by Mistake?
Deny the DELETE permission on the table and grant delete rights through a role. Maintain SQL Server Audit on the table.
Final Verdict
Your log is the only place that still stores a past delete and the login behind it. Copy it first and query it before the next log backup or checkpoint.
Use fn_dblog for a quick check and the log backup when the online log is clear. Microsoft documents the log and its truncation on its transaction log page and the audit syntax on its CREATE DATABASE AUDIT SPECIFICATION page.
