Deleting an AddressBook in nextcloud using sql

Needed a quick way to batch delete my contacts after some bad imports.

assuming you know what your address book name is you can delete with the following

SELECT @addressbookid:=id FROM `oc_addressbooks` WHERE principaluri = 'principals/users/someusername' AND uri = 'someaddressbookname';
DELETE FROM `oc_addressbookchanges` WHERE addressbookid = @addressbookid;
DELETE FROM `oc_cards` WHERE addressbookid=@addressbookid;
DELETE FROM `oc_cards_properties` WHERE addressbookid=@addressbookid;
DELETE FROM `oc_addressbooks` WHERE id=@addressbookid;

UPDATE: Additional cleanup of birthdate calendar was needed. if you know the id of your birthdate calendar use the following:

select @calendarid:=12345;

DELETE
FROM `oc_calendarobjects_props`
where objectid in (
  SELECT
    co.id
  FROM  `oc_calendarobjects` co
    left join `oc_cards` c on SUBSTRING_INDEX(SUBSTRING(co.uri, POSITION('-' IN co.uri)+1), '.ics',1)=c.uri 
  where co.calendarid=@calendarid
    and c.id is null
);

DELETE co
FROM  `oc_calendarobjects` co
    left join `oc_cards` c on SUBSTRING_INDEX(SUBSTRING(co.uri, POSITION('-' IN co.uri)+1), '.ics',1)=c.uri 
  where co.calendarid=@calendarid
    and c.id is null;