Bytearray Methods

bytearray.clear()

Remove every byte in place, preserving the identity of the bytearray. All aliases see the empty buffer. Clearing a nonempty buffer while a memoryview exports it raises BufferError. In CPython 3.13.5 clearing an already empty exported buffer succeeds because its length need not change. Use clear rather than assigning a new bytearray when aliases must observe the change.

Formal Syntax & Structure

python syntaxpython
data.clear()

Return Value

None

None; modifies the existing buffer.

Working Example

Example Snippetpython
data = bytearray(b"ABC")alias = dataprint(data.clear())print(list(alias), alias is data)print(data.clear())
Python Reference