Deleting Records
Danger
☠️ The commands listed here cause irrecoverable data loss! Only proceed if you know what you’re doing and you have a backup!
Note
The list of commands below is not exhaustive. If you can’t find what you’re looking for here, you are encouraged to ask the community.
Deleting Tickets (and their articles)
# Delete a ticket (specified by database ID) >> Ticket.find(4).destroy # Delete all tickets >> Ticket.destroy_all # Keep some tickets (specified by database ID); delete the rest >> tickets_to_keep = [1, 2, 3] >> Ticket.where.not(id: tickets_to_keep).destroy_all
Deleting Customers
Warning
Customers may not be deleted while they have tickets remaining in the system.
As such, the examples below will delete not only the specified customers, but all tickets associated with them, as well.
- Step 1: Select customers by email address
-
>> customer_emails = %w[customer@example.com customer@example.org] >> customers = User.joins(roles: :permissions).where(email: customer_emails, roles: { active: true }, permissions: { name: 'ticket.customer', active: true }).where.not(id: 1)
- Step 2: Preview affected users & tickets
-
>> puts customers.map { |user| <<~PREVIEW }.join("\n") Customer #{user.fullname}/#{user.id}/#{user.email} has #{Ticket.where(customer_id: user.id).count} tickets #{Ticket.where(customer_id: user.id).pluck(:number)} PREVIEW
- Step 3: Proceed with deletion
-
>> customers.find_each do |user| puts %{Preparing deletion of customer "#{user.fullname}" (and #{Ticket.where(customer_id: user.id).count} associated tickets)} Ticket.where(customer: user).find_each do |ticket| puts " Deleting ticket ##{ticket.number}..." ticket.destroy end puts " Removing references for user with email #{user.email}..." ActivityStream.where(created_by_id: user.id).update_all(created_by_id: 1) History.where(created_by_id: user.id).update_all(created_by_id: 1) Ticket::Article.where(created_by_id: user.id).update_all(created_by_id: 1) Ticket::Article.where(updated_by_id: user.id).update_all(updated_by_id: 1) Store.where(created_by_id: user.id).update_all(created_by_id: 1) StatsStore.where(created_by_id: user.id).update_all(created_by_id: 1) Tag.where(created_by_id: user.id).update_all(created_by_id: 1) OnlineNotification.find_by(user_id: user.id)&.destroy! puts " Deleting #{user.fullname}..." user.destroy end
Deleting Organizations
Note
Deleting an organization does not delete associated customers.
- Step 1: Select organizations
-
# by "active" status >> organizations = Organization.where(active: false) # by name >> organizations = Organization.where(name: 'Acme') # by partial match on notes >> organizations = Organization.where('note LIKE ?', '%foo%')
- Step 2: Preview affected organizations
-
>> puts organizations.map { |org| "ORGANIZATION #{org.name}" }.join("\n")
- Step 3: Proceed with deletion
-
>> organizations.each do |org| puts %{Preparing deletion of organization "#{org.name}"...} org.members.each do |member| puts " Removing #{member.fullname} from organization..." member.update!(organization_id: nil) end puts " Deleting #{org.name}..." org.destroy end
Deleting System Records
# Remove all online notifications >> OnlineNotification.destroy_all # Remove all entries from the Activity Stream (dashboard) >> ActivityStream.destroy_all # Remove entries for all recently viewed objects (tickets, users, organizations) >> RecentView.destroy_all # Remove all history information from tickets, users and organizations (dangerous!) >> History.destroy_all