Question
Can I remove a field such as userId from payloads that are sent to destinations in Segment?
Product
Twilio Segment
Environment
Segment Console
Answer
Yes, you can remove specific fields (including userId or custom identifiers) from payloads sent to your destinations by using Destination Insert Functions. Please note that the resulting payload will still need to satisfy the particular field requirements of the downstream destination, or else it could create delivery failures.
While standard Destination Filters are useful for dropping entire events or top-level properties, some destinations may automatically inject or enforce certain identity fields like userId. Implementing a custom Insert Function allows you to recursively scan the entire event payload and strip out specified keys before the data is transmitted to the destination.
Additional Information
How to Implement This Solution
In your Segment workspace, navigate to Connections > Destinations and select the destination you want to modify.
Click on the Functions tab and choose to create or edit a Destination Insert Function.
Here is an example of a recursive JavaScript function to clean your payloads. This code will inspect the event object at every level (including nested objects and arrays) and delete the specified keys.
/**
* Recursively removes specified sensitive keys from an object or array.
* @param {Object} obj - The event payload or nested property to clean.
*/
function cleanPayload(obj) {
if (obj === null || typeof obj !== 'object') return;
if (Array.isArray(obj)) {
for (const item of obj) cleanPayload(item);
return;
}
// Object.keys() only returns the object's own enumerable properties
for (const key of Object.keys(obj)) {
// Specify the keys you want to remove (e.g., 'userId')
if (key === 'userId') {
delete obj[key];
} else {
cleanPayload(obj[key]);
}
}
}
/**
* Handle track event
* @param {SegmentTrackEvent} event
* @param {FunctionSettings} settings
*/
async function onTrack(event, settings) {
// Learn more at https://segment.com/docs/connections/spec/track/
// Remove sensitive fields like userId before sending to destination
cleanPayload(event);
// Log the cleaned event for verification
console.log(event);
return event;
}
Test & Deploy: Use the built-in test console in the Segment Functions editor to send a mock event, verify that the
userIdis successfully removed from the output, and deploy the function to your live destination stream.