You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: firestore-bigquery-export/guides/EXAMPLE_QUERIES.md
+42Lines changed: 42 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -115,6 +115,10 @@ If you want to clean up data from your `changelog` table, use the following
115
115
`DELETE` query to delete all rows that fall within a certain time period,
116
116
e.g. greater than 1 month old.
117
117
118
+
#### Option 1: Remove stale changelog records but keep latest change per document (default)
119
+
120
+
If you want to remove all entries that are over one month old, regardless of whether they are the latest change for a document (e.g., including DELETE operations), use the following query:
121
+
118
122
```sql
119
123
/* The query below deletes any rows below that are over one month old. */
@@ -132,3 +136,41 @@ WHERE (document_name, timestamp) IN
132
136
AND DATETIME(t.timestamp) < DATE_ADD(CURRENT_DATETIME(), INTERVAL -1 MONTH)
133
137
)
134
138
```
139
+
140
+
⚠️ Note: This query will remove all entries older than one month, including the most recent record for documents whose last change (e.g., a DELETE) happened more than a month ago. Use this only if you do not need to retain full historical state in your changelog table.
141
+
142
+
#### Option 2: Remove all changelog records older than one month — including latest DELETE operations
143
+
144
+
If you want to remove all entries that are over one month old, regardless of whether they are the latest change for a document (e.g., including DELETE operations), use the following query:
145
+
146
+
```sql
147
+
/* Deletes all changelog records older than one month, including latest DELETEs */
WHERE DATETIME(timestamp) < DATE_ADD(CURRENT_DATETIME(), INTERVAL -1 MONTH)
150
+
```
151
+
152
+
#### Option 3: Remove all changelog records older than one month, including latest DELETE operations only
153
+
154
+
This option removes all old records, and it will also delete DELETE operations even if they are the latest change for a document — as long as they are older than one month.
155
+
156
+
Use this if you want to aggressively clean up deleted documents from your changelog, even if that means latest views will no longer reflect that those documents were deleted.
157
+
158
+
```sql
159
+
/* Deletes any changelog records over one month old,
160
+
including DELETEs that are the latest entry for a document */
WHERE (t.timestamp!=latest.timestampORt.operation='DELETE')
172
+
AND DATETIME(t.timestamp) < DATE_ADD(CURRENT_DATETIME(), INTERVAL -1 MONTH)
173
+
)
174
+
```
175
+
176
+
⚠️ Note: This will remove DELETE records that are older than one month even if they are the most recent change. As a result, your \_latest view will no longer show that those documents were deleted — they may appear as if they never existed. Use this option only if that behavior is acceptable for your use case.
0 commit comments