@@ -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 {
0 commit comments