bytearray.clear()

Empties the existing buffer rather than making a new one. Everyone holding a reference sees it empty — which is the reason to use it, and the reason to be careful.

Bytearray methodPython 3.3+Live demo
Common call
buf.clear()
Returns
None — the buffer is emptied in place
Replaces
del buf[:] and buf[:] = b""
Watch out
buf = bytearray() does NOT do the same thing
bytearray.clear()
→ None

Demo

Live evaluation
Try:
Inputs
sstrbuffer (as text)
Output
[b := bytearray(bytes('abc', 'utf-8')), b.clear(), b][2]
bytearray(b'')

Every case ends the same way, with an empty buffer — that is the whole method. What the output cannot show is the important part: the buffer object itself is unchanged, so any other name pointing at it now sees it empty too. Clearing an already-empty buffer is a harmless no-op.

Common patterns

Reuse one buffer across batches
Avoids allocating a fresh buffer every round.
for batch in batches:
    buf.clear()
    buf.extend(encode(batch))
Reset a shared buffer
The reason to prefer clear over rebinding — other holders observe it.
self._pending.clear()
Drop data after flushing
Send, then empty, keeping the same object.
sock.sendall(buf)
buf.clear()

Examples

1. Empties it
b = bytearray(b'abc') b.clear() b
Returns
bytearray(b'')
2. Returns None
bytearray(b'abc').clear()
Returns
None
3. Already empty
b = bytearray() b.clear() b
Returns
bytearray(b'')
4. Identity kept
b = bytearray(b'a') i = id(b) b.clear() id(b) == i
Returns
True
5. Aliases see it
a = bytearray(b'ab') c = a a.clear() c
Returns
bytearray(b'')
6. Rebinding does not
a = bytearray(b'ab') c = a a = bytearray() c
Returns
bytearray(b'ab')

Pitfalls

1. clear() and = bytearray() are different
The distinction that matters. clear empties the object everyone shares; rebinding only moves your own name and leaves other references pointing at the old, still-full buffer.
Others still see data
a = bytearray(b'ab')
c = a
a = bytearray()
c
bytearray(b'ab')
Everyone sees empty
a = bytearray(b'ab')
c = a
a.clear()
c
bytearray(b'')
2. It returns None
Assigning the result replaces your buffer with None — the same trap as append, extend and reverse.
Buffer lost
buf = buf.clear()
None
Just call it
buf.clear()
buf is empty
3. A live memoryview blocks it
Clearing resizes the buffer, and an open memoryview export forbids resizing. The BufferError names the export rather than the clear, which makes it puzzling.
Export blocks it
v = memoryview(buf)
buf.clear()
BufferError: Existing exports of data: object cannot be re-sized
Release the view
v.release()
buf.clear()
works
4. Python 3.3 and newer only
bytearray.clear arrived later than the type itself. Older code uses del buf[:], which you will still meet in the wild.
Fails on 3.2
buf.clear()
AttributeError: 'bytearray' object has no attribute 'clear'
Portable form
del buf[:]
works everywhere

When to use

Use it
  • Reusing a buffer across iterations instead of reallocating
  • Emptying a buffer other code also holds a reference to
  • Resetting a buffer attribute without rebinding it
Reach for something else
  • You want a fresh, independent buffer → buf = bytearray()
  • Removing only some bytes → rebuild with a comprehension
  • A memoryview is open on it → release the view first

Notes

Complexity
O(1) amortised — the length is set to zero
Return
None; the bytearray is mutated in place and keeps its identity
CPython impl
Objects/bytearrayobject.c :: bytearray_clear
Memory
May release the internal buffer; capacity is not guaranteed to be kept
Thread-safe
Not safe under concurrent mutation of the same buffer

FAQ

clear empties the existing object; rebinding points your name at a new one. If anything else refers to the original — an attribute, another variable, a queue — clear affects it and rebinding does not.

a = bytearray(b'ab')
c = a
a.clear()
c   # bytearray(b'')

History

3.0
bytearray introduced as the mutable counterpart to bytes.
3.3
clear and copy added, matching the list interface.