Completed tasks are automatically purged from the CCDB; the frequency of the purge is configurable in the TAS tile "Cloud Controller" tab:
By setting this value lower than the default of 31 days, the maximum size of the tasks queue will be proportionally smaller. To change the setting, enter the desired value in the field (pictured above), Save the change, and Apply Changes to TAS tile.
To connect to the CCDB:
https://community.pivotal.io/s/article/How-to-Connect-to-the-Pivotal-Application-Service-Internal-MySQL-database?language=en_US
To see the number of tasks in the CCDB task queue, you can execute the following query:
SELECT app_guid, COUNT(*) FROM tasks GROUP BY app_guid ORDER BY 2
The cloud_controller source code specifies the default behavior:
https://github.com/cloudfoundry/cloud_controller_ng/blob/8bb3f137f493aeb951962b37f62a2cc115d024a8/app/jobs/runtime/prune_completed_tasks.rb#L17
The variable cutoff_age_in_days
comes from clock_global cloud_controller_ng.yml
https://github.com/cloudfoundry/capi-release/blob/d1e89befd672046ef5f2f208478a28d948ccc7b6/jobs/cloud_controller_clock/spec#L109