# 能否恢复已删除的自定义用户字段？

**URL:** https://meta.discourse.org/t/can-i-undelete-a-custom-user-field/102255
**Category:** Support
**Tags:** user-custom-fields
**Created:** [2018年十一月17日 23:45 UTC](https://meta.discourse.org/t/can-i-undelete-a-custom-user-field/102255 "2018-11-17T23:45:10Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tophee](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tophee/32/73406_2.png) [@tophee](https://meta.discourse.org/u/tophee)
#### Post date: [2018年十一月17日 23:45 UTC](https://meta.discourse.org/t/can-i-undelete-a-custom-user-field/102255/1 "2018-11-17T23:45:10Z")

</div>

我不小心删除了一个自定义用户字段。这是否意味着它已从数据库中完全删除，还是仍然存在？

如果它仍然存在，是否有办法恢复它（除了还原备份）？或者能否通过数据浏览器访问其内容？

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [2018年十一月18日 09:40 UTC](https://meta.discourse.org/t/can-i-undelete-a-custom-user-field/102255/2 "2018-11-18T09:40:15Z")

</div>

I’m 95% sure a deleted custom field is gone forever.

If you can’t see it in [data explorer](https://meta.discourse.org/t/32566?silent=true), it’s not there anymore.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2018年十一月18日 17:13 UTC](https://meta.discourse.org/t/can-i-undelete-a-custom-user-field/102255/3 "2018-11-18T17:13:00Z")

</div>

Just a thought, but surely there would be a way to recreate the Custom Field and hack together a script to retrieve and refill the data from within a backup using a temporary cloud server to write out the data from the original Custom Field?

---

<div class="post-metadata">

### Author: ![tophee](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tophee/32/73406_2.png) [@tophee](https://meta.discourse.org/u/tophee)
#### Post date: [2018年十一月18日 17:48 UTC](https://meta.discourse.org/t/can-i-undelete-a-custom-user-field/102255/4 "2018-11-18T17:48:14Z")

</div>

Thanks for your thoughts. Yes, that’s obviously what backups are for. I don’t know what the smartest procedure would be for restoring user field content for, say, thousands of users. For me, the number the easiest procedure was to just do it manually. And that was actually surprisingly easy, so I mention it here (from memory, so I may have missed some detail):

- When you download the backup file and open it for example with good old WinRAR, you’ll find another archive in there, so open that with WinRAR too. You’ll find an `.sql` file.
- And now comes the good part: you can just open it as a text file (with the built-in WinRAR viewer or otherwise).
- Search for the name of the field or some content you hapoen to know and you’ll find the table for that user field.
- Now the only trickyness is that (at least in my case) the user ID is not the first but the second value in each row (I’d be curious what the first value represents (“COPY”)).

---

<div class="post-metadata">

### Author: ![Nacho\_Caballero](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nacho_caballero/32/130189_2.png) [@Nacho\_Caballero](https://meta.discourse.org/u/Nacho_Caballero)
#### Post date: [2020年六月12日 08:35 UTC](https://meta.discourse.org/t/can-i-undelete-a-custom-user-field/102255/5 "2020-06-12T08:35:40Z")

</div>

目前，删除自定义字段（如果其 `field_type` 为 `dropdown`，则涉及表 `user_fields` 和 `user_fields_options`）并不会删除用户的实际值（表 `user_custom_fields`）。因此，一个快速（但可能具有风险：请先备份）的变通方法是：

1. 手动创建一个新的自定义字段。
2. 将新 `user_fields` 中的 id 替换为旧的 id（这需要数据库的管理员权限）：`UPDATE user_fields SET id=1 WHERE id=3;`（假设 3 是新 id，1 是旧 id）。
3. 如果该字段是下拉菜单类型，则需要在 `user_field_options` 表中执行类似操作。
4. 如果有用户在你修复之前注册了站点并填写了新字段（或任何现有用户填写了新字段），你还需要修复这些记录。
