Recovering Deleted Data

How to Recover Deleted Data From a SQL Server Database

Learn how to recover deleted SQL Server data from backups, point-in-time restores, and temporal history, and which checks to make before restoring anything.

On this page
  1. Recovery depends on which copies of the data exist
  2. Check whether the delete is still open
  3. Stop anything that writes to the database
  4. Find out what backups exist
  5. Restore to a new database, not over the original
  6. Copy back only the missing rows
  7. Check temporal history, change tracking, and audit tables
  8. Transaction log tools as a last resort
  9. Recovery in Azure SQL Database
  10. Permissions you need to restore
  11. Prevent the next accident
  12. When to bring in help

Recovery depends on which copies of the data exist

SQL Server has no recycle bin for rows or tables. A committed DELETE, TRUNCATE TABLE, or DROP TABLE can’t be reversed with a single command. What you can recover depends on which copies exist: a backup chain, transaction log backups that cover the time of the change, a temporal history table, change tracking or change data capture, audit triggers, or a copy of the database made before the change.

Is it possible to recover permanently deleted data? Sometimes. The sections below run from the quickest checks to the most involved.

Check whether the delete is still open

If the delete ran inside a transaction that nobody committed, a rollback undoes it. In the same session, run:

SELECT @@TRANCOUNT AS open_transactions;

A result above zero means a transaction is still open. ROLLBACK TRANSACTION; undoes its changes, including any other work in that transaction, so check what else it touched first. A result of zero means the change was committed, and you’ll need one of the routes below. For a shorter walkthrough of this check, see how to undo an accidental deletion.

Stop anything that writes to the database

Pause scheduled jobs, ETL loads, and application writes if you can. New writes add log records and can reuse space that still holds the old rows. Keep the damaged database in place, rather than dropping it or restoring over it, so you have something to compare against.

Find out what backups exist

Start by checking the recovery model and the backup history. The recovery model decides whether SQL Server keeps the log records you’d need to restore to a particular moment.

SELECT name, recovery_model_desc
FROM sys.databases;

SELECT TOP (20) database_name, type, backup_start_date, backup_finish_date
FROM msdb.dbo.backupset
WHERE database_name = N'Sales'
ORDER BY backup_finish_date DESC;

In the type column, D is a full backup, I is a differential backup, and L is a transaction log backup.

Under the full or bulk-logged recovery model, log backups let you restore to a moment just before the change. Bulk-logged has limits when bulk operations were logged inside a log backup. Under the simple recovery model, SQL Server keeps no log backups, so you can only restore to the end of a full or differential backup. If the change happened after your last backup, the rows aren’t in any backup you have, but the records may still be in the transaction log.

Restore to a new database, not over the original

Restoring over the original replaces its current data, and Microsoft’s documentation notes that the effects of a restore can’t be undone. Restore under a new name instead, then compare the two databases. A restore also can’t run inside an open transaction, so make sure the session is clear first.

  1. If the database is online and uses the full or bulk-logged recovery model, back up its log now. That backup includes the change, so you can stop the restore just before it.
  2. Run RESTORE FILELISTONLY to read the logical file names in the backup. Then restore the last full backup taken before the change under a new name, using NORECOVERY so more backups can follow.
  3. Restore any differential backup taken after that full backup, again with NORECOVERY.
  4. Restore each log backup in order. Use NORECOVERY on the earlier logs. On the log that spans the change, use STOPAT with a time just before it, and finish with RECOVERY.
RESTORE DATABASE SalesRecovery
FROM DISK = N'D:\Backups\Sales_full.bak'
WITH MOVE N'Sales' TO N'D:\Data\SalesRecovery.mdf',
     MOVE N'Sales_log' TO N'D:\Data\SalesRecovery_log.ldf',
     NORECOVERY;

RESTORE LOG SalesRecovery
FROM DISK = N'D:\Backups\Sales_log_1400.trn'
WITH STOPAT = '2026-11-20 13:55:00', RECOVERY;

Replace the names and paths with the ones from your own backups. STOPAT stops at the last commit at or before the time you give, so pick that time from your application logs or from the moment just before the first missing row changed. The time has to fall within the range covered by that log backup.

Copy back only the missing rows

With the copy online, look at the newest rows still in the table to see where the gap starts. How to retrieve last 5 records in SQL shows the query. Then compare the copy with the live table and insert only the rows that are missing. A LEFT JOIN with a NULL check finds them. Set IDENTITY_INSERT only when you’re inserting explicit values into an identity column.

SET IDENTITY_INSERT dbo.Orders ON;

INSERT INTO dbo.Orders (OrderID, CustomerID, OrderDate, Total)
SELECT r.OrderID, r.CustomerID, r.OrderDate, r.Total
FROM SalesRecovery.dbo.Orders AS r
LEFT JOIN dbo.Orders AS o
    ON o.OrderID = r.OrderID
WHERE o.OrderID IS NULL;

SET IDENTITY_INSERT dbo.Orders OFF;

Before you run the insert, check the row counts and totals in the copy against what you expect. Child rows can be missing too, if the deleted rows were parents with cascading deletes. If the keys are new to you, see what is a pk and fk. The LEFT JOIN here is one of several join types; for the rest, see what are the four types of joins in sql.

Two caveats matter. If a row changed after the delete, the copy holds an older version, and the insert skips it because its key still exists, so compare those rows by hand. Also, triggers and foreign keys on the live table can reject rows, so test the insert on a copy first. Treat the recovery database as a staging area: check it, then move only what you need. For the general idea, see what is staging in a database.

Check temporal history, change tracking, and audit tables

Some setups keep the old data without a restore.

  • Temporal tables. Available in SQL Server 2016 and later. When a row is deleted from a system-versioned table, SQL Server moves the previous version into the history table and records when that version stopped being valid. To see how the table looked before a change, query it with FOR SYSTEM_TIME AS OF:
SELECT *
FROM dbo.Orders FOR SYSTEM_TIME AS OF '2026-11-20 13:55:00'
WHERE OrderID = 1042;

Insert the rows you need back into the current table, not into the history table. The history table only holds changes made after versioning was turned on, so earlier deletes won’t appear there.

  • Change data capture. If it was enabled before the change, its change tables keep the column values for inserts, updates, and deletes.
  • Change tracking. It records the primary keys of deleted rows but not their values. It tells you which rows went missing, but you still need a backup or a copy to get the data back.
  • Audit triggers. A trigger that copied deleted rows into an audit table keeps the values, if someone set it up before the change.

Transaction log tools as a last resort

SQL Server has an undocumented function, fn_dblog, that reads the active part of the transaction log. Microsoft doesn’t document or support it. Values in the log appear in hexadecimal, so turning them back into rows is slow and manual, and records that have already been cleared from the active log are out of its reach. Commercial log-reader tools exist too. Check what each one reads for your SQL Server version and recovery model before you buy or rely on it.

Run either route against a copy of the database, not production.

Recovery in Azure SQL Database

Azure SQL Database doesn’t use T-SQL RESTORE for point-in-time recovery. Its automatic backups keep point-in-time restore data for seven days by default. You can set the window from 1 to 35 days, but Basic databases are limited to 7 days.

A deleted database can be restored to its deletion time or to an earlier point within that window, on the same logical server. Azure takes a final log backup before it deletes a database. If the logical server itself was deleted, its databases can’t be recovered this way, unless you configured long-term retention for them. You run the restore from the Azure portal, the Azure CLI, Azure PowerShell, or the REST API.

Permissions you need to restore

Restores need specific server-level permissions. The database role db_owner isn’t enough by default. For an existing database, you need membership in the sysadmin or dbcreator server role, or you need to be the owner of that database. A database name that doesn’t exist yet needs CREATE DATABASE permission. The details are in can db_owner restore a database.

If you can’t run the restore yourself, ask whoever administers the instance, often a DBA, to run the steps above.

Prevent the next accident

  • Use the full recovery model for databases where point-in-time restore matters, and take log backups often enough to fit how much data you can afford to lose.
  • Test restores on a schedule. A backup you’ve never restored is an untested assumption.
  • Limit DELETE, DROP, and TRUNCATE rights to the people and jobs that need them. Keep in mind which commands change data and which change structure: DELETE is a DML command, while DROP TABLE is DDL. The dbms languages list groups the commands by type.
  • For tables where deleted rows matter, turn on temporal history or an audit trigger before the next incident.
  • Run manual deletes inside an explicit transaction, check @@ROWCOUNT against what you expected, and commit only when the count is right.
  • If a warehouse table is loaded by ETL from a source system, reloading from the source may be quicker than a restore. For how the warehouse itself is set up, see how to build a data warehouse in sql server.

When to bring in help

If the database is in production, or the data is regulated or personal, bring in a DBA before you run restores or log tools. Keep a record of each command you ran, when you ran it, and which copies you made.

Keep exploring

Guides from other topics

All topics →