How to delete certain tickets in one-go?

Infos:

  • Used Zammad version: 6.0
  • Used Zammad installation type: (source, package, docker-compose, …) package
  • Operating system: Debian 10.10
  • Browser + version:
    Chrome, Edge, Firefox, private windows, to no avail

Expected behavior:

  • We can run the command similar to Ticket.find_by(number:20802).destroy but not one by one, I need to run the command to select multiple tickets, e.g. I wanted to delete ticket number 20800 till 20900, what is the command to accomplish this?

Actual behavior:

  • I didn’t find the correct command to delete ticket number 20800 till 20900

Steps to reproduce the behavior:

run rail console to delete ticket number 20800 till 20900?

Edit by MrGeneration:
Please have a backup before executing the following commands.
They will destroy data without any further warning and option to bring it back.

Dangerzone

zammad run rails c

15] pry(main)> (20800..20900).to_a[0..5]
=> [20800, 20801, 20802, 20803, 20804, 20805]
[16] pry(main)> (20800..20900).to_a[-5..]
=> [20896, 20897, 20898, 20899, 20900]
[17] pry(main)> (20800..20900).each{|num| Ticket.find_by(number: num)&.destroy }
=> 20800..20900

The number column is a string, so we could try it via loop. As you can see this would be a loop inclusive over a 20800 to 20900.

If you have a specific list of ticket numbers then you can also do it like this:

[21] pry(main)> Ticket.where(number: ['20800', '20801']).each{|ticket| ticket.destroy }
=> []
1 Like

@rolfschmidt Thanks a lot for the information, this is more than helpful!

What I don’t understand is “have a backup before executing the following commands.” how can we back up any of these tickets and if we deleted the wrong tickets, there is a way to bring them back?

There is no rollback. I guess @MrGeneration meant that you should snapshot your server or backup the db before.

https://docs.zammad.org/en/latest/appendix/backup-and-restore/index.html

Exactly this is what I tried to add to Rolfs original answer.
I wanted to outline the potential danger in these commands to people that might not be aware.

Thanks a lot @rolfschmidt and @MrGeneration

All good and all clear now! Thanks again!

There is a quite simple way to delete tickets through the web interface rather than using the console. You need a macro and an automation task:
First you create a Marco which adds a tag of your choice to the ticket like “deleteicket” and sets the ticket to closed. Optional you can assign the ticket to a group which is only visible for a specific user group.
Second, you create an automation which deletes the tickets which match the tag, has the state closed and belongs to the specific group if you might have set.
So every ticket which matches the criteria would be deleted when the automation runs.

Finally to delete tickets you can go the overview, select the tickets, drag and drop them on the desired macro and when the next automation is performed, the tickets will be deleted.

1 Like

@germeshausen This looks like a way better option indeed. Is using a macro safer than DB operation?

I hadn’t had the need to delete a ticket yet, but I assume if one server ‘hosts’ millions of tickets it might be necesary?

It is quite simple and I would guess more safe than passing the ID in the console.
Although I don’t want to provide console access to anyone else other than the server administrators. The server administrator is certainly not the same person than the zammad administrator.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.