The issue
Sometimes there may be Xray Watch(ex: watch-1) which has been deleted and cannot be found from Watch&Policy in UI or by API, but there is still existing violation data of the Watch in Xray scan list.
And it also fails to delete it by using “DELETE /xray/api/v2/watches/test_watch”, with error response:
{"error":"Failed to delete Watch: Watch was not found"}
Root Case
Normally to delete xray watch should follow two steps:
- Search watch by name or ID and will get response of “watch was not found ” if no results found;
- Delete the watch if it is found in step1, then all its violations will be cleaned, also.
But in the issue cases in #1, when checking the database, you will find that the watch_id exists in table ‘user_issues’ but there is no relevant data in table ‘watches’;
SELECT COUNT(*) FROM user_issues ui LEFT JOIN watches w ON w.external_id =
ui.watcher_id WHERE w.id IS NULL;
There will be the orphaned data in table user_issues if there are no records in table watches(Such as the clean process was interrupted), which leads to the failure to delete watch as normal clean steps.
Resolution
Use the following maintenance API which needs admin user permission to clean all the violation data left:
POST /xray/api/v1/maintenance/clean_user_issues_of_deleted_watches
It will return {"info":"all issues of deleted watches are deleted"}, and the orphaned data will be deleted.
Noted:
This API will clean all orphaned watch data rather than a single watch, which needs to operate on a lower period if the xray instance has huge data, and please monitor the Xray and DB servers’ resource consumption during the process..