# Entity limits and cleanup on a FiveM server

> Keep FiveM entity counts under control: why too many vehicles, peds and objects hurt sync, finding leaks, server-side counts and cleanup strategies.

Source: https://fam-tools.com/guides/fivem-entity-limits-cleanup · FAM Tools · published 2026-09-11 · updated 2026-09-11 · about 9 min

**Short answer.** Count entities on the server with `GetAllVehicles()`, `GetAllPeds()` and `GetAllObjects()`, watch how the numbers grow between restarts, and find the resources that create entities without deleting them. Fix the scripts (delete on job end, on resource stop, on player drop), add a conservative cleanup for abandoned vehicles with a warning, and lower population density if AI traffic is the load.

## Key points

- Count with `GetAllVehicles/Peds/Objects` on the server.
- Growing counts between restarts = a leak.
- Scripts should delete what they create.
- Cleanup with a warning, never mid-use.
- Population density settings control AI load.

Every networked vehicle, ped and object has to be synced to nearby players. Scripts that spawn things and never delete them — job vehicles, dropped props, test spawns — slowly fill the world until sync slows down and odd bugs appear. Counting and cleaning entities is basic server hygiene.

## Counting entities

*server.lua — /entities command for staff*

```lua
RegisterCommand('entities', function(src)
    local msg = ('vehicles %d · peds %d · objects %d'):format(#GetAllVehicles(), #GetAllPeds(), #GetAllObjects())
    if src == 0 then print(msg) else TriggerClientEvent('chat:addMessage', src, { args = { 'Entities', msg } }) end
end, true)
```

Run it after a restart and again a few hours later. Healthy servers level off; leaking ones keep climbing.

## Finding the leak

- Group vehicles by model and plate pattern — job vehicles often share a prefix.
- Check entity state bags or owners to see which script created them.
- Stop suspect resources one by one on a test server and watch the counts.

## Cleaning up safely

*server.lua — remove empty, unowned vehicles*

```lua
local function cleanup()
    TriggerClientEvent('chat:addMessage', -1, { args = { 'Server', 'Empty vehicles are cleared in 60 seconds.' } })
    Wait(60000)
    for _, veh in ipairs(GetAllVehicles()) do
        local occupied = false
        for seat = -1, 6 do
            if GetPedInVehicleSeat(veh, seat) ~= 0 then occupied = true break end
        end
        if not occupied and not Entity(veh).state.owned then
            DeleteEntity(veh)
        end
    end
end
```

Mark player-owned vehicles (for example with an `owned` state bag set by your garage) so cleanup never deletes them. Server-side spawning and orphan mode: [server-side entities](https://pityus.net/guides/fivem-server-side-entities).

## AI population

Traffic and pedestrians are entities too. Lower densities in busy servers, or disable AI population entirely with `onesync_population false` if your game mode does not need it — see [traffic and ped density](https://fam-tools.com/guides/fivem-traffic-ped-density).

## Questions

### How do I count entities on my FiveM server?

On the server, `#GetAllVehicles()`, `#GetAllPeds()` and `#GetAllObjects()`.

### Why do entity counts keep growing?

A script spawns entities without deleting them. Find it by watching counts while stopping resources.

### Should I auto-delete vehicles?

Only empty, unowned ones, with a warning beforehand.

### How do I turn off AI traffic?

Lower densities with scripts or set `onesync_population false` to disable population.

## Related

- https://fam-tools.com/guides/fivem-desync-network-issues
- https://fam-tools.com/guides/fivem-onesync-infinity
- https://fam-tools.com/guides/fivem-traffic-ped-density
- https://fam-tools.com/guides/fivem-server-performance
