# Delete users who have not be active in 60 days unless they are on our
# mailing list.  Use authorization_expires as a safety check, in case someone
# did not set the authorization_type properly.
create temporary table to_delete select id from users where authorization_type="none" and us_mail="N" and creation < now() - interval 60 day and authorization_expires="0000-00-00 00:00:00";
delete from symbol_lists using symbol_lists,to_delete where user_id=to_delete.id;
delete from symbols_in_lists using symbols_in_lists,to_delete where user_id=id;
delete from view_mru using view_mru,to_delete where user_id=id;
delete from link_destination using link_destination, to_delete where user_id=id;
delete from user_cookie using user_cookie, to_delete where user_id=id;
delete from user_deltas using user_deltas, to_delete where user_id=id;
delete from users using users, to_delete where users.id=to_delete.id;
#AX_windows does not appear to be necessary.
drop table to_delete;

# If someone was on our mailing list but has not be active in 60 days, then we just delete their symbol lists.
delete symbol_lists from symbol_lists,users left join user_cookie on users.id=user_cookie.user_id and invalidated_from is null where users.id=symbol_lists.user_id and greatest(creation,authorization_expires,cookie_valid_start) < now() - interval 60 day and authorization_type <> "sample";
delete symbols_in_lists from symbols_in_lists,users left join user_cookie on users.id=user_cookie.user_id and invalidated_from is null where users.id=symbols_in_lists.user_id and greatest(creation,authorization_expires,cookie_valid_start) < now() - interval 60 day and authorization_type <> "sample";
