Configure FFmpeg hls_flags to Delete Old Segments

When streaming live video using HTTP Live Streaming (HLS), keeping every generated TS video segment can quickly exhaust your server’s disk space. This article provides a quick and clear guide on how to configure the hls_flags parameter in FFmpeg to automatically delete old segments as new ones are created, allowing you to maintain a lightweight, continuous live stream.

To automatically delete old HLS segments, you must use the -hls_flags delete_segments option in your FFmpeg command. For this flag to work, you must also define a limit for your playlist size using -hls_list_size. If the list size is set to 0 (infinite), no segments will ever be deleted.

Example FFmpeg Command

Here is a standard command template to stream an input source and automatically delete old segments:

ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f hls -hls_time 6 -hls_list_size 5 -hls_flags delete_segments playlist.m3u8

Parameter Breakdown

How It Works

FFmpeg maintains a rolling queue of segments. With -hls_list_size 5, the playlist will only reference the 5 most recent segments. When segment 6 is created: 1. Segment 1 is removed from the playlist.m3u8 file. 2. Because delete_segments is enabled, FFmpeg immediately deletes the physical file for segment 1 from your storage.

This configuration guarantees that your storage usage remains constant, only holding a maximum of 5 to 6 segment files at any given time.