@@ -22,6 +22,12 @@ class Deployer {
2222
2323 const DEFAULT_NAMESPACE = 'static-deploy-addon-s3/default ' ;
2424
25+ /**
26+ * WordPress option recording when stale objects were last deleted,
27+ * and from which bucket and prefix
28+ */
29+ const STALE_OBJECTS_LAST_RUN_OPTION = 'static_deploy_s3_deleteStaleObjectsLastRun ' ;
30+
2531 private int $ cf_max_paths ;
2632
2733 /**
@@ -290,6 +296,9 @@ public function uploadFiles( string $processed_site_path ): void {
290296 * Delete objects under the bucket prefix that are not part of
291297 * the site, if the deleteStaleObjects option is set.
292298 *
299+ * Skipped if it already ran for the same bucket and prefix within
300+ * the deleteStaleObjectsCooldown period.
301+ *
293302 * Call this only after deploying the whole site. An object is part
294303 * of the site if its path was passed to uploadFilesIter by this
295304 * instance, or if it is in the crawled files. The crawled files
@@ -307,6 +316,23 @@ public function deleteStaleObjects(): void {
307316 return ;
308317 }
309318
319+ $ bucket = S3Options ::getValue ( 'bucketName ' );
320+ $ prefix = self ::s3Prefix ();
321+ $ target = $ bucket . '/ ' . $ prefix ;
322+ $ cooldown_hours = intval ( S3Options ::getValue ( 'deleteStaleObjectsCooldown ' ) );
323+ $ last_run = get_option ( self ::STALE_OBJECTS_LAST_RUN_OPTION );
324+ if ( $ cooldown_hours > 0
325+ && is_array ( $ last_run )
326+ && ( $ last_run ['target ' ] ?? null ) === $ target
327+ && time () - intval ( $ last_run ['time ' ] ?? 0 ) < $ cooldown_hours * HOUR_IN_SECONDS ) {
328+ WsLog ::l (
329+ 'Not deleting stale S3 objects because they were deleted ' .
330+ " less than {$ cooldown_hours } hours ago. "
331+ );
332+ return ;
333+ }
334+ $ started_at = time ();
335+
310336 $ current_keys = $ this ->current_keys ;
311337 foreach ( CrawledFiles ::getPathsIter () as $ path_info ) {
312338 if ( $ path_info ->status !== 404 ) {
@@ -315,8 +341,6 @@ public function deleteStaleObjects(): void {
315341 }
316342
317343 $ filtering = $ mode === 'except-ignored ' ? new FileFiltering () : null ;
318- $ bucket = S3Options ::getValue ( 'bucketName ' );
319- $ prefix = self ::s3Prefix ();
320344 $ pages = $ this ->s3_client ->getPaginator (
321345 'ListObjectsV2 ' ,
322346 [
@@ -343,6 +367,7 @@ public function deleteStaleObjects(): void {
343367
344368 if ( $ stale_keys === [] ) {
345369 WsLog ::l ( 'No stale S3 objects to delete. ' );
370+ $ this ->recordStaleObjectsDeleted ( $ target , $ started_at );
346371 return ;
347372 }
348373
@@ -390,9 +415,25 @@ public function deleteStaleObjects(): void {
390415
391416 WsLog ::l ( "Deleted $ deleted_ct stale S3 objects, $ error_ct failed. " );
392417
418+ // Record time even if some deletions failed because
419+ // listing the bucket can be expensive. It's okay if
420+ // some files stick around for the next deletion pass.
421+ $ this ->recordStaleObjectsDeleted ( $ target , $ started_at );
422+
393423 $ this ->invalidateStalePaths ();
394424 }
395425
426+ private function recordStaleObjectsDeleted ( string $ target , int $ time ): void {
427+ update_option (
428+ self ::STALE_OBJECTS_LAST_RUN_OPTION ,
429+ [
430+ 'target ' => $ target ,
431+ 'time ' => $ time ,
432+ ],
433+ false
434+ );
435+ }
436+
396437 /**
397438 * Returns the deploy cache key for a path, which is the
398439 * path of the file it is deployed to.
0 commit comments