code: def delete_invalid_tickets(self):
          
          for ticket in self.filter(Q(consumed__isnull=False) |
                                    Q(expires__lte=now())).order_by('-expires'):
              try:
                  ticket.delete()
              except models.ProtectedError:
                  pass
docs: Delete consumed or expired ``Ticket``s that are not referenced
          by other ``Ticket``s. Invalid tickets are no longer valid for
          authentication and can be safely deleted.
  
          A custom management command is provided that executes this method
          on all applicable models by running ``manage.py cleanupcas``.