Mass delete texts from iMessage on MacOS Mojavae

2 25
Avatar for jkister
4 years ago

I received over 1,000 text messages from random four-digit numbers like 2367, 3632, and 2346. I didn't want any of them.

it just took some Perl. i used a simple SQL LIKE of four underscores to match any sender that had four characters.

#!/usr/bin/perl

use strict;
use DBI;

my $SQL_PATTERN = '____';

my $dsn = "DBI:SQLite:dbname=$ENV{HOME}/Library/Messages/chat.db";
my $dbh = DBI->connect( $dsn, undef, undef );

my $sql = <<__EOS__
 SELECT message.rowid AS mid,
        chat.rowid    AS cid,
        text
   FROM message, chat, chat_message_join
  WHERE mid = chat_message_join.message_id
    AND cid = chat_message_join.chat_id
    AND chat.chat_identifier LIKE \'$SQL_PATTERN\'
__EOS__
;
my $sth = $dbh->prepare($sql);
$sth->execute;
while( my $row = $sth->fetchrow_hashref){
    #print "$row->{mid} <-> $row->{cid}\n";
    #print "message found: $row->{text}\n";
    $dbh->do( "DELETE FROM message WHERE message.ROWID = $row->{mid}" );
    $dbh->do( "DELETE FROM chat    WHERE chat.ROWID    = $row->{cid}" );
    $dbh->do( "DELETE FROM chat_message_join WHERE chat_id = $row->{cid} AND message_id = $row->{mid}" );
}

Then I had to restart some process, or maybe reboot, i forget. but it worked.

you could also modify the sql to delete texts that contain some string:

#!/usr/bin/perl

use strict;
use DBI;

my $SQL_PATTERN = '%free vacation%';

my $dsn = "DBI:SQLite:dbname=$ENV{HOME}/Library/Messages/chat.db";
my $dbh = DBI->connect( $dsn, undef, undef );

my $sql = <<__EOS__
 SELECT message.rowid AS mid,
        chat.rowid    AS cid,
        text
   FROM message, chat, chat_message_join
  WHERE mid = chat_message_join.message_id
    AND cid = chat_message_join.chat_id
    AND text LIKE \'$SQL_PATTERN\'
__EOS__
;
my $sth = $dbh->prepare($sql);
$sth->execute;
while( my $row = $sth->fetchrow_hashref){
    #print "$row->{mid} <-> $row->{cid}\n";
    #print "message found: $row->{text}\n";
    $dbh->do( "DELETE FROM message WHERE message.ROWID = $row->{mid}" );
    $dbh->do( "DELETE FROM chat    WHERE chat.ROWID    = $row->{cid}" );
    $dbh->do( "DELETE FROM chat_message_join WHERE chat_id = $row->{cid} AND message_id = $row->{mid}" );
}

In this case, all messages containing the phrase "free vacation" would be deleted.

find how to install perl from Google. Maybe perlbrew.

3
$ 0.50
$ 0.50 from @Read.Cash
Avatar for jkister
4 years ago

Comments

Cool! Though I haven't used Perl in like... maybe, 15 years or so? :)

$ 0.00
4 years ago

it's all about perl these days. my first love. :D

$ 0.00
4 years ago