When deleting a course in either a course category or on a course by course basis if the course is large the deletion happens on the webserver and since no update to the users UI is sent during this process the web server can timout and close the connection throwing the gateway timeout error with additional issues relating to session id's for the user if they attempt to continue working on the moodle instance. The user experience is too close the browser then open a new one and login again then repeat the same deletion process. This potentially can be extremely time consuming.
Create a number of courses in a category of varying sizes then delete them one at a time and check how long each deletion actually takes before the screen is updated or the users recevies the gateway time out error.
As a guide clients using moodle i support have courses ranging from a few kilobytes in size up to 21.5GB. Testing of approx 1000 courses with the recycle bin for course categories enabled and disabled have shown any course over approx 250MB will throw the gateway error.
What clients expect is the courses should be marked for deletion and ran in the background allowing the users to continue using moodle.
What actually happens is the user must stay at the UI waiting for the deletion to complete and sometimes receive the gateway time out error.
Referencing this previous tracker https://tracker.moodle.org/browse/MDL-18340