The following can be used in place of your original sentence:
Next, the mac variable is tested to determine if the system needs to delete the relevant data from the database when the route and direction (of what?) was detected by the WiFi AP.
Since you are trying to figure out (determine) when something needs to be done. Once it is figured out, the system then will perform an action (needs to). When describes the timing around the action.
Your sense of relevent is correct to use since the relevent data is what is focussed on, some of the data may be related but not relevent and would not need to be deleted.
Testing the mac variable implicitly figures out its value.
There may still be some ambiguity since the reader does not know what route and direction is being referred to. If it is any detection of route and direction, then in the event that can be used, but is more wordy than when.
...from the database in the event that the route and direction was detected...