File tree 1 file changed +15
-4
lines changed
1 file changed +15
-4
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ class CleanEmails extends Command
12
12
13
13
protected $ description = 'Clean up old incoming email logs. ' ;
14
14
15
+ protected $ amountDeleted = 0 ;
16
+
15
17
public function handle ()
16
18
{
17
19
$ this ->comment ('Cleaning old incoming email logs... ' );
@@ -29,13 +31,22 @@ public function handle()
29
31
/** @var InboundEmail $modelClass */
30
32
$ modelClass = config ('mailbox.model ' );
31
33
32
- $ models = $ modelClass ::where ('created_at ' , '< ' , $ cutOffDate )->get ();
34
+ // chunk the deletion to avoid memory issues
35
+
36
+ $ this ->amountDeleted = 0 ;
33
37
34
- $ models ->each ->delete ();
38
+ $ modelClass ::where ('created_at ' , '< ' , $ cutOffDate )
39
+ ->select ('id ' )
40
+ ->chunk (100 , function ($ models ) use ($ modelClass ) {
41
+ foreach ($ models as $ model ) {
42
+ $ modelInstance = $ modelClass ::find ($ model ->id );
43
+ $ modelInstance ->delete ();
44
+ $ this ->amountDeleted ++;
45
+ }
46
+ });
35
47
36
- $ amountDeleted = $ models ->count ();
37
48
38
- $ this ->info ("Deleted {$ amountDeleted } record(s) from the Mailbox logs. " );
49
+ $ this ->info ("Deleted {$ this -> amountDeleted } record(s) from the Mailbox logs. " );
39
50
40
51
$ this ->comment ('All done! ' );
41
52
}
You can’t perform that action at this time.
0 commit comments