I am using Mysql and by mistake deleted very important details.Is there a way how to recover deleted records in Mysql?
-
1A hint: make a new column with a boolean. If that boolean is 0, the record has deleted, if 1 it is not deleted. Update always that column and you never lose data. Don't use a `DELETE` statement. – H. Pauwelyn Aug 27 '15 at 08:36
-
Another option would be to keep a backup database which holds deleted records, if you do decide to actually using `DELETE`. – Tim Biegeleisen Aug 27 '15 at 08:37
-
@Luïs can you detail a bit, what can i do in the place of Deleting the info, so by human mistake i deleted real info mwanted to delete test one but deleted the real – Ana DEV Aug 27 '15 at 08:51
-
@Tim Biegeleisen how can create a backup Db for deleted records?manually or in some automatic way? – Ana DEV Aug 27 '15 at 08:58
-
https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html – Tim Biegeleisen Aug 27 '15 at 08:58
-
Ah it is serious thing.Thank you for the link.I am new with that – Ana DEV Aug 27 '15 at 09:03
-
Off topic. Try dba.stackexchange.com. I know it's too late, but the answer is backup, backup, backup. MySQL will do it for you automatically. I have daily MySQL backups going back to 2009. – user207421 Aug 27 '15 at 09:30
-
@EJP Oh yes I know but the human fact happened.Your Db backed up automatically in a daily base? – Ana DEV Aug 27 '15 at 09:31
4 Answers
I know this is not really what you are asking but I find it important that you know this.
The system I suggest is called soft delete. It works as follow:
Make a new column in your table (for ex the table people):
ID Name Deleted 1 Bob 0 2 Frank 1 3 Alice 0 With 0 as undeleted and 1 as deleted in last column with the type
BOOLEAN
. Name and ID are respectively aNVARCHAR
and anINT
.If you want delete a record, you don't delete it with a
DELETE
statement but update it like this:UPDATE people SET Deleted = 1 WHERE ID = 1
Result:
ID Name Deleted 1 Bob 1 2 Frank 1 3 Alice 0 You know ID's 1 and 2 are deleted because the boolean in column
Deleted
is on1
. Alice with ID 3 is not deleted because the boolean is on0
.
The pro of this system is that you can recover data with only one statement!
UPDATE people SET Deleted = 0 WHERE Deleted = 1
Result:
ID | Name | Deleted |
---|---|---|
1 | Bob | 0 |
2 | Frank | 0 |
3 | Alice | 0 |
Now all record are recovered because the boolean in column Deleted
is on 0.
If you use DELETE
statement, it is impossible to recover all records! They are deleted for always and you can never recover it if you are using the DELETE
statements. You can only use a backup file for recover it but has also contra's like:
- It's an old backup file,
- I've forget to make one,
- I don't know how you can make it,
- I've lost it,
- ...
By soft delete you change only one column and you have your data back.
Edit:
The contra of the system is that (like you have said) the data is not exactly remove from your database. It's only a column you change from 0 to 1. But if you know that you can make money from data... is this another story.
If you want to delete it exactly you can use a DELETE
statement.

- 13,575
- 26
- 81
- 144
-
Thank you very much for the detailed info, very useful one.So for first I need to add a column Deleted.But in fact after command UPDATE people SET Deleted = 1 WHERE ID = 1 info didnt deleted from Db?My aim with Delete is to not have heavy Db with odd info and that's why I was deleteing records – Ana DEV Aug 27 '15 at 09:51
-
1Yes, Luïs is suggesting that you do not ever actually `DELETE` any rows, you just have a deleted column containing either a 1 or 0. If the value is 1, then the column is deleted. So you just select your data `WHERE deleted = 0`. This would prevent any problems with lost data rows – Reece Kenney Aug 27 '15 at 09:58
-
yes, I know that. It is a contra to use big data. But if you know that data is money... You go think about money. If you would delete it exactly, you can also use a `DELETE` statement alter the `UPDATE` if the user that want. – H. Pauwelyn Aug 27 '15 at 10:00
-
We use this a lot, but we're beginning to question our decision because it really doesn't work well with unique keys. We can't mix up `is_deleted` (bool) and `email` (unique). If an account is deleted, its email or username can never be reused. – Vincent Poirier Dec 08 '15 at 20:50
-
1Lolled at 'big data'. Implementing a soft delete doesn't make this big data ;) – Evert Nov 04 '20 at 07:52
-
Do you have binlog files from the date where you insert these records ?
Then you can get the inserts and put it back

- 14,525
- 2
- 24
- 39
-
1
-
1MySQL write any "writes" in a binary logfile. this is usable for replication or recovery here you can found details https://mariadb.com/kb/en/mariadb/binary-log/ – Bernd Buffen Aug 27 '15 at 09:24
-
-
1Can you help me to figure out from where I need to open these files, how can I get the info pls @Bernd Buffen – Ana DEV Aug 27 '15 at 09:30
-
yes, every binlog file has a number in the Filename like -rw-rw---- 1 mysql mysql 1073882317 Aug 1 16:31 binlog.0076 -rw-rw---- 1 mysql mysql 1074239062 Aug 1 19:13 binlog.0077 so, in the file .0077 are all change between 16:31 and 19:13 from 1.8.2015 and you can look into the file with the command mysqlbinlog - # mysqlbinlog binlog.007607 |less mysqlbinlog has many option to filter the file or you can use grep – Bernd Buffen Aug 27 '15 at 09:51
-
-
normaly they are in the datadir. you can show where this is SHOW VARIABLES LIKE 'datadir'; – Bernd Buffen Aug 27 '15 at 09:56
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88079/discussion-between-anahit-dev-and-bernd-buffen). – Ana DEV Aug 27 '15 at 10:13
-
mysql> use employees;
Database changed
mysql> show create table employees;
| employees | CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` enum('M','F') NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
mysql> select count(*) from employees;
+----------+
| count(*) |
+----------+
| 1000 |
+----------+
1 row in set (0.00 sec)
mysql> delete from employees;
mysql> flush table employees with read lock;
Query OK, 0 rows affected (0.01 sec)
we can directly scan the table .ibd file ,and find the deleted records:
select add .ibd from menu start
input the deleted table's ibd location
right click the ibd file , scan it
click the first page node , and input create table sql
choose the right mysql version , and page format (dynamic in most version)
select the Deleted Data Tab , then you can check deleted records and export them into MYSQLDUMP format SQL file.
You can also use conventional recovery mode in this case.

- 83
- 1
- 3
For InnoDB tables, if binary logs are enabled, one could use:
mysqlbinlog path_to_binary_log_file > query_log.sql
If binary logs are not enabled, there is a recovery tool provided by Percona which may help.
Created a script to automate the steps from the Percona tool which recovers the deleted rows (if exist) and provides SQL queries to load the data back into database.
Please note:
The time between the deletion of rows and the database stop is crucial. If pages are reused you can’t recover the data.
PS: the DBRecover Tool by @ParnassusData is cool and much easier IMO

- 3,130
- 3
- 27
- 41