How to delete data in gun DB?
Asked Answered
C

1

10

I have been developing some things and you know during early prototyping types and tables change quickly... it would be nice to cleanup the old data and start again in certain meshes.

For now I was using the example HTTP server so I deleted data.json; but I forgot the localStorage in the browser also needs to be cleared.

One might suppose you could put(null)

I asked on gitter and got

https://github.com/amark/gun/wiki/delete

except for deletes , lol, our excuse is "It works like your OS, when you delete >something it just gets tossed in the trash/recycle bin. That's all." better safe than sorry though

if you are trying to "delete" stuff because you messed up while developing >something, follow this three step process: 1) localStorage.clear() in every >browser tab you have up, 2) Crash the server and rm data.json, 3) restart >everything. You should then have a clean slate. Often times while I'm >devleoping something I put localStorage.clear() at the top of my code so I only >have to worry about clearing the server.

Canteen answered 10/6, 2016 at 23:55 Comment(0)
J
10

Welcome to the gun community! Thanks for asking questions.

Yes, deleting data is most simply done with gun.put(null). Take:

var gun = Gun();
var users = gun.get('users');
users.put({alice: {name: 'alice'}, bob: {name: 'bob'}});
// now let's delete bob
users.path('bob').put(null);

If (as you mentioned in the question) however, you mean "delete data" as in wanting to clear out mistakes while developing your app. You'll want to do what you mentioned: localStorage.clear() in all browsers, crash the all servers and rm data.json.

For other developers, it might be useful to know that gun uses a type of tombstone method. You can't actually delete nodes themselves, they just get de-referenced, kinda like how your OS just moves files into a trash/recycle-bin. This tombstone method is very important in a distributed environment, such that the "delete" operation is replicated to every peer.

Thanks for answering your own question though! As always, if you get lost or need help hop on the https://gitter.im/amark/gun .

Justness answered 11/6, 2016 at 0:10 Comment(4)
gun().put(null) seems like the most general answer, since gun can now run in places without localStorage (like on react-native using async-storage)Ulm
@Justness - We try to use gun in a settings where events are collected and processed. They are only important in the first n hours, then they should be discarded. We could use put to replace the event with a null, but will this scale? At some point, map would have to deal with a gazillion id's pointing to null. Is there a strategy for this?Barncard
Not at all, just generate new buckets, gun.get('list/2022/2/4').set(...), gun.get('list/2022/2/5').set(...), gun.get('list/2022/2/6').set(...) ...Justness
using store.path(key).put(null) actually removes the key from Node. Instead, store.get(key).put(null) just nulls the value under that key, but the key is still listed in node.map(). Therefore the only way I found to actually delete the key when the value is set to null is .path(key).put(null)Heavyfooted

© 2022 - 2024 — McMap. All rights reserved.