🤠 Wild Country Libs
Guide

Safe Resource Cleanup

NPCs, blips, prompts, vehicles, and mission state that outlive the interaction they belonged to are the single most common source of "ghost" bugs in RedM resources. wc_libs' Flow helpers exist specifically to make cleanup a one-line call instead of a checklist you forget half of.

Use this pattern any time a resource spawns world entities (peds, blips, markers, GPS routes) tied to a single interaction — a job run, a dialogue, an encounter. If nothing is spawned into the world, you likely don't need a cleanup bag.

The cleanup bag pattern

CreateCleanupBag gives you a single object to register everything you spawn against, and a single Clean() call that tears it all down in the right order (prompts hidden and deleted, blips/GPS cleared, then peds/vehicles/objects deleted).

client
local bag = wc:CreateCleanupBag()

local npc = wc:SpawnPed(hash, x, y, z, heading, true, true)
bag:AddPed(npc)

local prompt = wc:CreatePrompt("Talk [G]")
bag:AddPrompt(prompt)

bag:AddBlip(wc:CreateMissionMarker(x, y, z, "Encounter", { route = true }))
bag:AddGPS()

-- when the interaction ends, whatever the reason:
bag:Clean()

Wire cleanup to every exit path

The bag itself is idempotent — calling Clean() twice is harmless — so wire it into every possible ending, not just the "happy path":

Server-side state cleanup

Cleanup isn't just visual — server-tracked mission state (see the job guide's ActiveRuns table) needs the same treatment on disconnect, or you'll leak memory and can permanently block a player from starting a new run.

server
AddEventHandler('playerDropped', function()
  ActiveRuns[source] = nil
  ActiveDeliveries[source] = nil
end)

Vehicle cleanup

Use DeleteWagon or DeleteVehicle — never the raw DeleteVehicle native directly — since both unmark the mission-entity flag and unfreeze first, which the raw native won't do for you.

client
local wagon = wc:SpawnWagon('WAGON02X', x, y, z, heading, { isMission = true })
bag:AddVehicle(wagon) -- CleanupBag calls DeleteVehicle/DeletePed appropriately based on entity type

Common mistakes