1+ // Copyright 2026 Datalust Pty Ltd and Contributors
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // http://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
14+
15+ using System ;
16+ using System . Threading . Tasks ;
17+ using SeqCli . Api ;
18+ using SeqCli . Cli . Features ;
19+ using SeqCli . Config ;
20+ using Serilog ;
21+ // ReSharper disable UnusedType.Global
22+
23+ namespace SeqCli . Cli . Commands ;
24+
25+ [ Command ( "events" , "delete" , "Delete log events that match a given date range or filter" ,
26+ Example = "seqcli events delete --start \" 2026-01-01T00:00:00Z\" --end \" 2026-01-31T23:59:59Z\" " ) ]
27+ class DeleteCommand : Command
28+ {
29+ readonly ConnectionFeature _connection ;
30+ readonly DateRangeFeature _range ;
31+ readonly SignalExpressionFeature _signal ;
32+ readonly StoragePathFeature _storagePath ;
33+ string ? _filter ;
34+
35+ public DeleteCommand ( )
36+ {
37+ Options . Add (
38+ "f=|filter=" ,
39+ "A filter to apply to deletion, for example `Host = 'xmpweb-01.example.com'`" ,
40+ v => _filter = v ) ;
41+
42+ _range = Enable < DateRangeFeature > ( ) ;
43+ _storagePath = Enable < StoragePathFeature > ( ) ;
44+ _signal = Enable < SignalExpressionFeature > ( ) ;
45+
46+ _connection = Enable < ConnectionFeature > ( ) ;
47+ }
48+
49+ protected override async Task < int > Run ( )
50+ {
51+ try
52+ {
53+ var config = RuntimeConfigurationLoader . Load ( _storagePath ) ;
54+ var connection = SeqConnectionFactory . Connect ( _connection , config ) ;
55+
56+ string ? filter = null ;
57+ if ( ! string . IsNullOrWhiteSpace ( _filter ) )
58+ filter = ( await connection . Expressions . ToStrictAsync ( _filter ) ) . StrictExpression ;
59+
60+ await connection . Events . DeleteAsync (
61+ null ,
62+ _signal . Signal ,
63+ filter ,
64+ _range . Start ,
65+ _range . End ,
66+ null ) ;
67+
68+ Log . Information ( "Deleted matching events" ) ;
69+
70+ return 0 ;
71+ }
72+ catch ( Exception ex )
73+ {
74+ Log . Error ( ex , "Could not delete matching events: {ErrorMessage}" , ex . Message ) ;
75+ return 1 ;
76+ }
77+ }
78+ }
0 commit comments