# Come recuperare un argomento cancellato usando la console Rails

**URL:** https://meta.discourse.org/t/how-to-recover-a-deleted-topic-using-the-rails-console/112421
**Category:** Support
**Created:** [23 Marzo 2019, 7:51pm UTC](https://meta.discourse.org/t/how-to-recover-a-deleted-topic-using-the-rails-console/112421 "2019-03-23T19:51:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![will\_io](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/will_io/32/147541_2.png) [@will\_io](https://meta.discourse.org/u/will_io)
#### Post date: [23 Marzo 2019, 7:51pm UTC](https://meta.discourse.org/t/how-to-recover-a-deleted-topic-using-the-rails-console/112421/1 "2019-03-23T19:51:33Z")

</div>

This is how the accidental deletion occurred:

```ssh
cd /var/discourse
./launcher enter app
rails c
Topic.where(:id=><id>).destroy_all

```

The `<id>` I deleted was the wrong one and of course it just so happened to be a topic that a number of users were tuned into it. The important thing is I stopped, did not panic and did not execute another action after the accidental deletion occurred.

🛑_Stop, 🚫 Do Not Panic and 🚫 Do Not Execute_

How I recovered the accidentally deleted topic using the rails console:

```plaintext
destroyed_topic = _  
destroyed_topic.each do |destroyed_record|
  Topic.create(destroyed_record.attributes)
end

```

For future reference are there other ways could I have achieved this? If so, how?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [24 Marzo 2019, 11:32pm UTC](https://meta.discourse.org/t/how-to-recover-a-deleted-topic-using-the-rails-console/112421/2 "2019-03-24T23:32:20Z")

</div>

È difficile prevedere con certezza, ma `destroy` dovrebbe essere riservato esclusivamente a casi assolutamente confermati. Effettua prima un backup. Non sono sicuro che il recupero in questo caso possa propagarsi e restituire tutti i record necessari.

Invece:

```plaintext
Topic.find_by(id: <id>).trash!

```

Per recuperare:

```plaintext
Topic.with_deleted.where(id: <id>).recover!

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [4 Maggio 2021, 8:51am UTC](https://meta.discourse.org/t/how-to-recover-a-deleted-topic-using-the-rails-console/112421/3 "2021-05-04T08:51:30Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
