Skip to content

Commit 1010a79

Browse files
authored
cmd/geth: make it possible to autopilot removedb (#28725)
When managing geth, it is sometimes desirable to do a partial wipe; deleting state but retaining freezer data. A partial wipe can be somewhat tricky to accomplish. This change implements the ability to perform partial wipe by making it possible to run geth removedb non-interactive, using command line options instead.
1 parent cfff3cb commit 1010a79

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

cmd/geth/dbcmd.go

+28-6
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,22 @@ import (
4343
)
4444

4545
var (
46+
removeStateDataFlag = &cli.BoolFlag{
47+
Name: "remove.state",
48+
Usage: "If set, selects the state data for removal",
49+
}
50+
removeChainDataFlag = &cli.BoolFlag{
51+
Name: "remove.chain",
52+
Usage: "If set, selects the state data for removal",
53+
}
54+
4655
removedbCommand = &cli.Command{
4756
Action: removeDB,
4857
Name: "removedb",
4958
Usage: "Remove blockchain and state databases",
5059
ArgsUsage: "",
51-
Flags: utils.DatabaseFlags,
60+
Flags: flags.Merge(utils.DatabaseFlags,
61+
[]cli.Flag{removeStateDataFlag, removeChainDataFlag}),
5262
Description: `
5363
Remove blockchain and state databases`,
5464
}
@@ -211,11 +221,11 @@ func removeDB(ctx *cli.Context) error {
211221
}
212222
// Delete state data
213223
statePaths := []string{rootDir, filepath.Join(ancientDir, rawdb.StateFreezerName)}
214-
confirmAndRemoveDB(statePaths, "state data")
224+
confirmAndRemoveDB(statePaths, "state data", ctx, removeStateDataFlag.Name)
215225

216226
// Delete ancient chain
217227
chainPaths := []string{filepath.Join(ancientDir, rawdb.ChainFreezerName)}
218-
confirmAndRemoveDB(chainPaths, "ancient chain")
228+
confirmAndRemoveDB(chainPaths, "ancient chain", ctx, removeChainDataFlag.Name)
219229
return nil
220230
}
221231

@@ -238,14 +248,26 @@ func removeFolder(dir string) {
238248

239249
// confirmAndRemoveDB prompts the user for a last confirmation and removes the
240250
// list of folders if accepted.
241-
func confirmAndRemoveDB(paths []string, kind string) {
251+
func confirmAndRemoveDB(paths []string, kind string, ctx *cli.Context, removeFlagName string) {
252+
var (
253+
confirm bool
254+
err error
255+
)
242256
msg := fmt.Sprintf("Location(s) of '%s': \n", kind)
243257
for _, path := range paths {
244258
msg += fmt.Sprintf("\t- %s\n", path)
245259
}
246260
fmt.Println(msg)
247-
248-
confirm, err := prompt.Stdin.PromptConfirm(fmt.Sprintf("Remove '%s'?", kind))
261+
if ctx.IsSet(removeFlagName) {
262+
confirm = ctx.Bool(removeFlagName)
263+
if confirm {
264+
fmt.Printf("Remove '%s'? [y/n] y\n", kind)
265+
} else {
266+
fmt.Printf("Remove '%s'? [y/n] n\n", kind)
267+
}
268+
} else {
269+
confirm, err = prompt.Stdin.PromptConfirm(fmt.Sprintf("Remove '%s'?", kind))
270+
}
249271
switch {
250272
case err != nil:
251273
utils.Fatalf("%v", err)

0 commit comments

Comments
 (0)