Removing an Item from an Array in Clarity: The Hack I Wish I Knew Earlier

Table of contents
Hey there 👋
So this is officially my first ever Clarity tutorial article (more like my first ever tutorial article 😅), and I thought, why not start with something that really gave me a hard time when I was just starting out?
If you’ve written smart contracts in Clarity before, you’ve probably noticed one thing really quickly: arrays (or rather, lists) in Clarity are immutable, and there’s no built-in way to just remove an item. No pop()
, no remove()
, no sweet JavaScript-like magic. Just you, the list, and some good old functional programming.
I first ran into this wall while building a time-locked savings contract, the idea was to let parents lock up funds for their kids until a future date. I added an admin management system where certain operations (like emergency withdrawal) could only be done by designated admins.
Adding admins? Easy. Removing an admin? That’s where things got tricky.
💡 The Problem
I needed to remove a specific admin from a list of admins. But Clarity doesn't let you remove from lists directly. So I had to rebuild a new list from the old one, excluding the admin I wanted to remove.
🛠️ The Solution
Enter: fold
.
Using fold
, I looped through the list and conditionally added items to a new list, basically filtering out the admin I wanted to remove.
Here’s how I did it:
I had the main function to remove-child-admin, then I created a private helper function (remove-admin) to filter out the admin I want to remove by creating a new list.
(define-private (remove-admin (list-admin principal) (admin-tracker {to-remove: principal, new-admin-list: (list 5 principal)}))
(merge admin-tracker {new-admin-list:
(if (is-eq list-admin (get to-remove admin-tracker))
(get new-admin-list admin-tracker)
(unwrap-panic (as-max-len? (append (get new-admin-list admin-tracker) list-admin) u5))
)}
)
)
And in the remove-child-admin
function, I used that inside a fold
:
(map-set child-account {parent: parent, child-name: child-name}
(merge
current-child-account
{admins: (get new-admin-list (fold remove-admin current-child-admins {compare-to: admin-to-remove, new-admin-list: (list)}))}
)
)
It’s basically like saying:
"Hey, go through every admin in the list. If it’s not the one we want to remove, add it to a new list. If it is the one, skip it."
🎉 Final Thoughts
Was it a bit messy the first time? Oh yes.
Did I eventually feel like a wizard when it worked? Absolutely.
If you’re also working on Clarity contracts and bump into this kind of thing, know that you’re not alone. And if this helped you out even a little, I’d love to hear about it!
Let me know what other Clarity tricks you'd like to see explained. I’ve got a few more battle stories up my sleeve. 😄
Thanks for reading! Feel free to connect with me on Twitter, always down to chat Clarity, Stacks, or just say hi.
Subscribe to my newsletter
Read articles from Muritadhor Arowolo directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
