@@ -150,14 +150,21 @@ fn powersync_clear_impl(
150
150
) -> Result < String , PowerSyncError > {
151
151
let local_db = ctx. db_handle ( ) ;
152
152
153
- let clear_local = args[ 0 ] . int ( ) ;
153
+ let flags = PowerSyncClearFlags ( args[ 0 ] . int ( ) ) ;
154
+
155
+ if !flags. soft_clear ( ) {
156
+ // With a soft clear, we want to delete public data while keeping internal data around. When
157
+ // connect() is called with compatible JWTs yielding a large overlap of buckets, this can
158
+ // speed up the next sync.
159
+ local_db. exec_safe ( "DELETE FROM ps_oplog; DELETE FROM ps_buckets" ) ?;
160
+ } else {
161
+ local_db. exec_safe ( "UPDATE ps_buckets SET last_applied_op = 0" ) ?;
162
+ }
154
163
155
164
// language=SQLite
156
165
local_db. exec_safe (
157
166
"\
158
- DELETE FROM ps_oplog;
159
167
DELETE FROM ps_crud;
160
- DELETE FROM ps_buckets;
161
168
DELETE FROM ps_untyped;
162
169
DELETE FROM ps_updated_rows;
163
170
DELETE FROM ps_kv WHERE key != 'client_id';
@@ -166,7 +173,7 @@ DELETE FROM ps_stream_subscriptions;
166
173
" ,
167
174
) ?;
168
175
169
- let table_glob = if clear_local != 0 {
176
+ let table_glob = if flags . clear_local ( ) {
170
177
"ps_data_*"
171
178
} else {
172
179
"ps_data__*"
@@ -199,6 +206,22 @@ DELETE FROM {table};",
199
206
Ok ( String :: from ( "" ) )
200
207
}
201
208
209
+ #[ derive( Clone , Copy ) ]
210
+ struct PowerSyncClearFlags ( i32 ) ;
211
+
212
+ impl PowerSyncClearFlags {
213
+ const MASK_CLEAR_LOCAL : i32 = 0x01 ;
214
+ const MASK_SOFT_CLEAR : i32 = 0x02 ;
215
+
216
+ fn clear_local ( self ) -> bool {
217
+ self . 0 & Self :: MASK_CLEAR_LOCAL != 0
218
+ }
219
+
220
+ fn soft_clear ( self ) -> bool {
221
+ self . 0 & Self :: MASK_SOFT_CLEAR != 0
222
+ }
223
+ }
224
+
202
225
create_auto_tx_function ! ( powersync_clear_tx, powersync_clear_impl) ;
203
226
create_sqlite_text_fn ! ( powersync_clear, powersync_clear_tx, "powersync_clear" ) ;
204
227
0 commit comments