[rt-devel] Re: [rt-users] Removing Deleted Tickets

Jonas Lincoln lincoln at unit.liu.se
Tue Jun 3 06:18:54 EDT 2003


I've received a script from Wash working in RT2. I've now modified it to 
work with RT3, and it seems to do everything correct.

Script is attached.

ODHIAMBO Washington wrote:
> * Jonas Lincoln <lincoln at unit.liu.se> [20030603 10:21]: wrote:
> 
>>What is the correct way to go if I want to remove all tickets marked as 
>>"deleted"?
> 
> 
> 
> 
> Use the script attached:
> 
> 
> -Wash
> 
-------------- next part --------------
#!/usr/bin/perl
#
# Steve Poirier - <A HREF="mailto:steve at inet-technologies.com">steve at inet-technologies.com</A>
# edited by Tim Allwine <A HREF="mailto:tallwine at oreilly.com">tallwine at oreilly.com</A>
# to use with RT2 and MySQL
# edited by Tony Aiuto <tony at ics.com> to deal with Links
# edited by Jonas Lincoln <lincoln at unit.liu.se> to work with RT3 and mysql

use strict;
my $ticketnbr = 0;

# Replace this with your RT_LIB_PATH
use lib "/service/rt3/lib";
# Replace this with your RT_ETC_PATH
use lib "/service/rt3/etc";

use RT::Interface::CLI  qw(CleanEnv loc
			   GetCurrentUser GetMessageContent);
use RT::Group;
use RT;

#Clean out all the nasties from the environment
CleanEnv();

#Load etc/config.pm and drop privs
RT::LoadConfig();

#Connect to the database and get RT::SystemUser and RT::Nobody loaded
RT::Init();

(my $x=RT::Handle->new)->Connect;

my $dbh = $x->dbh() or die "Couldn't connect to RT database";

my $sql = q{SELECT id FROM Tickets where Status=?};
my $sth = $dbh->prepare($sql);
$sth->execute('deleted');


# proceed with deletion

my $d_sth     = $dbh->prepare('SELECT id FROM Transactions where Ticket=?');
my $stid      = $dbh->prepare('DELETE FROM Attachments WHERE TransactionId=?');
my $sttrans   = $dbh->prepare( 'DELETE FROM Transactions WHERE Ticket=?');
my $stwatch   = $dbh->prepare( 'DELETE FROM Watchers WHERE Value=?');
my $stobjects = $dbh->prepare( 'DELETE FROM ObjectKeywords WHERE ObjectId=?');
my $stlinks1  = $dbh->prepare( 'DELETE FROM Links WHERE LocalBase=?');
my $stlinks2  = $dbh->prepare( 'DELETE FROM Links WHERE LocalTarget=?');
my $stticket  = $dbh->prepare( 'DELETE FROM Tickets WHERE id=?');

while ( my ($ticketid) = $sth->fetchrow_array() )  {
    print "\nticket $ticketid is dead. Deleting:  ";
    $d_sth->execute($ticketid);

# delete Attachments for this ticket
    print "Attachments. ";
    while ( my ($transid) = $d_sth->fetchrow_array() )  {
	$stid->execute($transid);
    }

# delete Ticket in Transactions table
    print "Transactions. ";
    $sttrans->execute($ticketid);

# delete Ticket in ticket in Watchers table
    print "Watchers. ";
    $stwatch->execute($ticketid);

# delete Ticket in ObjectKeywords table
    print "ObjectKeywords. ";
    $stobjects->execute($ticketid);

# delete Ticket from the Links table
    print "Links1. ";
    $stlinks1->execute($ticketid);
    print "Links2. ";
    $stlinks2->execute($ticketid);

# delete Ticket from the Tickets table
    print "Ticket. ";
    $stticket->execute($ticketid);

    $ticketnbr++;
}
print "\nDone. Deleted $ticketnbr tickets.\n\n";

exit;


More information about the Rt-devel mailing list