- Home
- Tutorials
- Flash
- Intermediate
- SharedObjects

Deleting SharedObjects and tidying up
Jesse Stratford
Jesse lives and works in Melbourne Australia. He is the Cofounder of http://ActionScript.org. A Flash enthusiast, teacher, author, freelancer and speaker Jesse's main focus nowadays is managing http://ActionScript.org, but he enjoys participating actively in community and the wider Flash scene when he has time.
View all articles by Jesse StratfordmySO = SharedObject.getLocal("test");
mySO.data.stickAround = "I'll be here for years to come!";
mySO.flush();
delete mySO;
// Reload the SO
mySO = SharedObject.getLocal("test");
delete mySO.data.stickAround;
mySO.flush();
// Delete the SO
delete mySO;
// Load the SO back in once more,
mySO = SharedObject.getLocal("test");
// Scan the SO for values
for (a in mySO.data) {
trace(a+": "+mySO.data[a]);
}
// Wohoo! No stickAround value!
Macromedia's docs don't show us how to remove the physical file from a user's drive, (although they say it's possible), but we can combine two things we know already to figure it out. Firstly, when we write a SO with no properties in its data child, no file is written. Secondly when we delete a property from the data child and re-write the SO the property is removed from the file on the disk also. So in order to delete the actual file, one need only delete all the properties of the data child. This is probably best done with a for...in loop.
// Make a basic SO
mySO = SharedObject.getLocal("test");
mySO.data.stickAround = "I'll be here for years to come!";
mySO.data.myArray = new Array(1, 2, 3, 4);
mySO.flush();
delete mySO;
// Reload the SO
mySO = SharedObject.getLocal("test");
for (a in mySO.data) {
delete mySO.data[a];
}
mySO.flush();
// Delete the SO
delete mySO;
// Load the SO back in once more,
mySO = SharedObject.getLocal("test");
// Scan the SO for values
for (a in mySO.data) {
trace(a+": "+mySO.data[a]);
}
// There are none. There's no file on the drive any more either
It's noteworthy that all the examples I've used in this tutorial cover creating and accessing the SO from the same SWF file. In practice this will rarely be the case, as we like to make our SWF files modular. To access a SO file created by a different SWF file you need to provide additional arguments to the getLocal() method. See this thread in which we show how it's done.
Finally, if you are determined to force your user to accept the SO you can advise them that SOs are required for your application and then run this code:
System.showSettings(1);
which will popup the local settings configuration box (see Figure 2.1) and allow the user to enable/disable SO storage and increase/decrease allocated SO storage space on a general scale.

Figure 2.1 Change Local Storage Settings Prompt
Viola! That's it! Thanks to Macromedia for their good docs on this. I'm off to bed. If this tutorial helped you out please drop me an email and let me know. I'm open to corrections and suggestions as always.
| Jesse Stratford is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash. NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there. |
If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity. |
| This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it. |
Spread The Word
6 Responses to "SharedObjects" 
|
said this on 08 Mar 2007 2:40:55 AM CST
Would've been nice if you had mentioned where the file gets stored. I thought it would be stored on the same folder as the fla. But then ran a search for it and found it deep inside the localhost folder.
|
|
said this on 02 Jun 2007 7:33:58 PM CST
Can you add a function for how to load the saved file? Like for a game.
|
|
said this on 09 Jun 2007 4:16:21 AM CST
thanks this code has solved my problem.
|
|
said this on 18 Jun 2007 1:21:42 AM CST
this tut help me lot to understand the sharedObject
but i am confused how i used it in swf file how I represent my file to any other |
|
said this on 06 Nov 2007 9:33:01 PM CST
To load for a game would be to add a button for retrieval purposes like this:
previous_btn.onRelease = function(){ previousEntry(); } In order to have this work for a game, you need to create the buttons to go with the content. Overall this in an interesting way to view the SharedObjects value. |
|
said this on 02 Jan 2008 11:56:24 AM CST
u could load your data from the shared object like this:
holder = mySO.data.name; text1.text = holder; then there goes.reload your data |




Author/Admin)