Introduction

備忘録。
過去に接続した履歴がぶら下がっているが、これを一度に消す方法がなくて困っていた。
一個一個消すのも面倒なので、自分で調べた次第。

How to resolve?

Windows に関してのみだが調べると、 %APPDATA%\Code\User\globalStorage\state.vscdb に色んなデータが入っていることが分かった。
このファイルはただの Sqlite のファイルなので適当なツールで覗いてみると、それらしいデータが json で入っていたので、構造を把握してデータを書き換えたところ上手くいった。

下記が自動化したスクリプト。
一応 Visual Studio Code を終了させて実行した方がいい。
試した環境は 1.100.2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import json
import os
import sqlite3

appRoaming = os.getenv('APPDATA')
db = os.path.join(appRoaming, "Code", "User", "globalStorage", "state.vscdb")

if not os.path.exists(db):
print(f"Error: missing {db}")
exit

conn = sqlite3.connect(db)
cur = conn.cursor()

cur.execute("SELECT value FROM ItemTable WHERE key='ms-vscode-remote.remote-ssh'")

rows = cur.fetchall()
if len(rows) == 1:
row = rows[0]
json_str = row[0]
json_dict = json.loads(json_str)

# clear all history
json_dict["folder.history.v1"] = {}

json_str = json.dumps(json_dict)

cur.execute('''
INSERT INTO ItemTable (key, value)
VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value=excluded.value
''', ("ms-vscode-remote.remote-ssh", json_str))
conn.commit()

cur.close()
conn.close()