-2

I deleted some data in a database but I want those data back.

Is there any way to restore the data from a database that was deleted using the rails console?

aalbagarcia
  • 1,019
  • 7
  • 20
  • 1
    Do you have backups of the DB somewhere? – Rockwell Rice Oct 30 '20 at 05:22
  • If you were using [transactions](https://www.tutorialspoint.com/sql/sql-transactions.htm) you could `rollback`. Otherwise restore from backup or [if it's Postgres you *might* be able to recover the data](https://stackoverflow.com/questions/12472318/can-i-rollback-a-transaction-ive-already-committed-data-loss). – Schwern Oct 30 '20 at 05:47

2 Answers2

1

Unfortunately, you cannot recover data using the console tool unless previously deleted objects have been saved to a variable or elsewhere.

0

No, you cannot recover any data that was deleted using the rails console. You will need a backup. If you don't have a backup, you will not be able to restore the data.

However, depending on how you removed your data, you might have had a chance to recover it at the moment you deleted it.

If you used ActiveRecord code like the following

Rails.application.eager_load!
ActiveRecord::Base.connection.disable_referential_integrity do
  ApplicationRecord.descendants.each do |model|
    model.delete_all
  end
end

The data is lost. (Have a look at this question and the answers about different ways to truncate data)

If you did something like this:

objects = MyModel.delete_all

then you might have had a chance to recover them right after that using the objects array.

aalbagarcia
  • 1,019
  • 7
  • 20