Skip to content

Commit ca3c417

Browse files
committed
add support for deleting all ephemeral devices
Signed-off-by: Lee Briggs <[email protected]>
1 parent abc6402 commit ca3c417

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

cmd/tscli/delete/devices/cli.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func Command() *cobra.Command {
4444
lastSeenDuration time.Duration
4545
exclude []string
4646
confirm bool
47+
ephemeral bool
4748
)
4849

4950
cmd := &cobra.Command{
@@ -62,6 +63,9 @@ Examples:
6263
# Show devices that would be deleted (default behavior)
6364
tscli delete devices --last-seen 15m
6465
66+
# Delete only ephemeral devices not seen for 1 hour
67+
tscli delete devices --last-seen 1h --ephemeral --confirm
68+
6569
# Delete devices not seen for 24 hours, excluding specific patterns
6670
tscli delete devices --last-seen 24h --exclude server --exclude prod --confirm
6771
@@ -74,7 +78,7 @@ Examples:
7478
return fmt.Errorf("failed to create client: %w", err)
7579
}
7680

77-
summary, err := deleteDisconnectedDevices(cmd.Context(), client, lastSeenDuration, exclude, confirm)
81+
summary, err := deleteDisconnectedDevices(cmd.Context(), client, lastSeenDuration, exclude, ephemeral, confirm)
7882
if err != nil {
7983
return fmt.Errorf("failed to delete devices: %w", err)
8084
}
@@ -94,13 +98,15 @@ Examples:
9498
"Duration to consider a device disconnected (e.g., 15m, 1h, 24h)")
9599
cmd.Flags().StringSliceVar(&exclude, "exclude", nil,
96100
"Device names to exclude by partial match (can be specified multiple times)")
101+
cmd.Flags().BoolVar(&ephemeral, "ephemeral", false,
102+
"Only delete ephemeral devices")
97103
cmd.Flags().BoolVar(&confirm, "confirm", false,
98104
"Actually delete devices (default is a dry run)")
99105

100106
return cmd
101107
}
102108

103-
func deleteDisconnectedDevices(ctx context.Context, client *tsapi.Client, lastSeenTimeout time.Duration, excludedDevices []string, confirm bool) (*DeletionSummary, error) {
109+
func deleteDisconnectedDevices(ctx context.Context, client *tsapi.Client, lastSeenTimeout time.Duration, excludedDevices []string, ephemeralOnly bool, confirm bool) (*DeletionSummary, error) {
104110
// List all devices with full details to get ephemeral status
105111
devices, err := client.Devices().ListWithAllFields(ctx)
106112
if err != nil {
@@ -119,6 +125,12 @@ func deleteDisconnectedDevices(ctx context.Context, client *tsapi.Client, lastSe
119125
continue
120126
}
121127

128+
// Check if we should only process ephemeral devices
129+
if ephemeralOnly && !device.IsEphemeral {
130+
skippedDevices = append(skippedDevices, fmt.Sprintf("%s (not ephemeral)", device.Name))
131+
continue
132+
}
133+
122134
// Calculate time since the device was last seen
123135
timeSinceLastSeen := now.Sub(device.LastSeen.Time)
124136
if timeSinceLastSeen > lastSeenTimeout {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
golang.org/x/oauth2 v0.30.0
1616
golang.org/x/term v0.33.0
1717
gopkg.in/yaml.v3 v3.0.1
18-
tailscale.com/client/tailscale/v2 v2.0.0-20250714184745-d692cd62b191
18+
tailscale.com/client/tailscale/v2 v2.0.0-20250721135607-5f581daaece7
1919
)
2020

2121
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,5 @@ tailscale.com/client/tailscale/v2 v2.0.0-20250616154411-35b8e02bd63e h1:X9wV8C5X
201201
tailscale.com/client/tailscale/v2 v2.0.0-20250616154411-35b8e02bd63e/go.mod h1:4akEJPbysqHWAP+t7CZLQ5ZH8/vZWeH6+Hv+fEJUMp0=
202202
tailscale.com/client/tailscale/v2 v2.0.0-20250714184745-d692cd62b191 h1:JE+IQOoOBQoli/OImRsEZt21/KMcA3komumwqINIUsc=
203203
tailscale.com/client/tailscale/v2 v2.0.0-20250714184745-d692cd62b191/go.mod h1:4akEJPbysqHWAP+t7CZLQ5ZH8/vZWeH6+Hv+fEJUMp0=
204+
tailscale.com/client/tailscale/v2 v2.0.0-20250721135607-5f581daaece7 h1:9rTFcSAHUmfMaOh8tO2+M3fgIumsPSDx61BoQtHZsqA=
205+
tailscale.com/client/tailscale/v2 v2.0.0-20250721135607-5f581daaece7/go.mod h1:4akEJPbysqHWAP+t7CZLQ5ZH8/vZWeH6+Hv+fEJUMp0=

0 commit comments

Comments
 (0)